UIWebView的使用說明:
一、顯示網頁: html
// 1.建立webview,並設置大小,"20"爲狀態欄高度 UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20)]; // 2.建立請求 NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.cnblogs.com/mddblog/"]]; // 3.加載網頁 [webView loadRequest:request]; // 最後將webView添加到界面 [self.view addSubview:webView];
二、UIWebView不只能夠加載HTML頁面,還支持pdf、word、txt、各類圖片等等的顯示。下面以加載mac桌面上的png圖片web
三、 代理協議使用:UIWebViewDelegatelua
/// 是否容許加載網頁,也可獲取js要打開的url,經過截取此url可與js交互 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSString *urlString = [[request URL] absoluteString]; urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSArray *urlComps = [urlString componentsSeparatedByString:@"://"]; NSLog(@"urlString=%@---urlComps=%@",urlString,urlComps); return YES; } /// 開始加載網頁 - (void)webViewDidStartLoad:(UIWebView *)webView { NSURLRequest *request = webView.request; NSLog(@"webViewDidStartLoad-url=%@--%@",[request URL],[request HTTPBody]); } /// 網頁加載完成 - (void)webViewDidFinishLoad:(UIWebView *)webView { NSURLRequest *request = webView.request; NSURL *url = [request URL]; if ([url.path isEqualToString:@"/normal.html"]) { NSLog(@"isEqualToString"); } NSLog(@"webViewDidFinishLoad-url=%@--%@",[request URL],[request HTTPBody]); NSLog(@"%@",[self.webView stringByEvaluatingJavaScriptFromString:@"document.title"]); } /// 網頁加載錯誤 - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSURLRequest *request = webView.request; NSLog(@"didFailLoadWithError-url=%@--%@",[request URL],[request HTTPBody]); }