|
<div id="cnblogs_post_body">iOS开发UIWebView展示网页实例代码
更多阅读请访问http://www.hopean.com
UIWebView是ios内置的浏览器控件,我们可以用它来浏览网页、打开文档等等。
代码如下
viewController.h
@interface ViewController : UIViewController<UIWebViewDelegate>{ UIWebView *webView; UIActivityIndicatorView *activityIndicatorView;}@end
viewController.m
- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; webView.delegate = self; webView.scalesPageToFit = YES; [self.view addSubview:webView]; activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame : CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)] ; [activityIndicatorView setCenter: self.view.center] ; [activityIndicatorView setActivityIndicatorViewStyle: UIActivityIndicatorViewStyleGray] ; [self.view addSubview : activityIndicatorView] ; NSURL *url =[NSURL URLWithString:@"http://www.hopean.com"]; NSURLRequest *request =[NSURLRequest requestWithURL:url]; [webView loadRequest:request];}- (void)webViewDidStartLoad:(UIWebView *)webView{ [activityIndicatorView startAnimating] ;}- (void)webViewDidFinishLoad:(UIWebView *)webView{ [activityIndicatorView stopAnimating];}- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{ UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"" message:[error localizedDescription] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alterview show]; [alterview release];} |
|