iOS中UIWebView展示网页代码
操作方法
- 01
在ios学习中大家经常会用到UIWebView,它是ios内置的浏览器控件,在ios教程中我们可以用它来浏览网页、打开文档等等,本篇为大家讲解一下如何应用UIWebView。 代码如下 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]; }