UIWebView是網頁視圖控件,用來顯示網頁內容。功能類似於浏覽器。
1、goBack功能使用時,需要在已經打開過第二層及以上子鏈接的情況下才能返回打開上一層的鏈接
2、goForward功能使用時,需要在已經實現goBack功能的情況下才能實現,即已經存在緩存功能
3、UIWebView是UIScrollView的子類,擁有scrollView的相關屬性
4、擴展進階功能:與JavaScript的交互
我的第一個標題
我的第一個段落。
- UIWebView*webview=[[UIWebViewalloc]initWithFrame:CGRectMake(0.0,0.0,CGRectGetWidth(self.view.bounds),(CGRectGetHeight(self.view.bounds)-40.0))];
- [self.viewaddSubview:webview];
- webview.backgroundColor=[UIColorbrownColor];
- //tag
- webview.tag=1000;
- //UIScrollView的子類的相關屬性
- webview.scrollView.scrollEnabled=YES;
- webview.scrollView.delegate=self;
- //加載網頁
- //方法1加載百度網頁
- //NSURL*url=[NSURLURLWithString:@"http://www.baidu.com"];
- //NSURLRequest*request=[NSURLRequestrequestWithURL:url];
- //[webviewloadRequest:request];
- //方法2加載網頁內容
- NSString*html=@"";
- [webviewloadHTMLString:htmlbaseURL:nil];
-
- //代理
- /*
- 1設置代理方法的實現對象
- 2添加協議
- 3實現代理方法
- */
- webview.delegate=self;
-
- //多按鈕視圖控件
- UISegmentedControl*segmentedControl=[[UISegmentedControlalloc]initWithItems:@[@"GoBack",@"GoForward",@"Reload"]];
- [self.viewaddSubview:segmentedControl];
- segmentedControl.frame=CGRectMake(0.0,(CGRectGetHeight(self.view.bounds)-40.0),CGRectGetWidth(self.view.bounds),40.0);
- [segmentedControladdTarget:selfaction:@selector(segmentedControllAction:)forControlEvents:UIControlEventValueChanged];
- segmentedControl.tag=2000;
- //活動狀態視圖
- UIActivityIndicatorView*activityView=[[UIActivityIndicatorViewalloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
- [self.viewaddSubview:activityView];
- activityView.color=[UIColorredColor];
- activityView.frame=CGRectMake(0.0,0.0,50.0,50.0);
- activityView.center=self.view.center;
- [activityViewstopAnimating];
- activityView.hidesWhenStopped=YES;
- activityView.tag=3000;
- -(void)segmentedControllAction:(UISegmentedControl*)segmented
- {
- UIWebView*webview=(UIWebView*)[self.viewviewWithTag:1000];
-
- NSIntegerindex=segmented.selectedSegmentIndex;
- switch(index)
- {
- case0:
- {
- //返回打開上一個頁面
- BOOLisGoBack=[webviewcanGoBack];
- if(isGoBack)
- {
- [webviewgoBack];
- }
- }
- break;
- case1:
- {
- //向前打開下一個頁面
- BOOLisGoForward=[webviewcanGoForward];
- if(isGoForward)
- {
- [webviewgoForward];
- }
- }
- break;
- case2:
- {
- //重新加載當前頁面
- BOOLisReloading=[webviewisLoading];
- if(!isReloading)
- {
- [webviewreload];
- }
- }
- break;
-
- default:
- break;
- }
- }
- @interfaceViewController()
-
- @end
-
- //UIWebViewDelegate
- -(BOOL)webView:(UIWebView*)webViewshouldStartLoadWithRequest:(NSURLRequest*)requestnavigationType:(UIWebViewNavigationType)navigationType
- {
- NSLog(@"request%@",request);
-
- //該方法可用來控制子鏈接
- //if(navigationType==UIWebViewNavigationTypeLinkClicked)
- //{
- ////禁止打開子鏈接
- //returnNO;
- //}
-
- returnYES;
- }
-
- -(void)webViewDidStartLoad:(UIWebView*)webView
- {
- NSLog(@"開始加載");
-
- BOOLisReloading=[webViewisLoading];
- UISegmentedControl*segmentedControl=(UISegmentedControl*)[self.viewviewWithTag:2000];
- if(isReloading)
- {
- segmentedControl.userInteractionEnabled=NO;
- }
- else
- {
- segmentedControl.userInteractionEnabled=YES;
- }
-
- UIActivityIndicatorView*activityView=(UIActivityIndicatorView*)[self.viewviewWithTag:3000];
- [activityViewstartAnimating];
- }
-
- -(void)webViewDidFinishLoad:(UIWebView*)webView
- {
- NSLog(@"加載完成");
-
- //加載成功,可用
- UISegmentedControl*segmentedControl=(UISegmentedControl*)[self.viewviewWithTag:2000];
- segmentedControl.userInteractionEnabled=YES;
-
- //加載成功,停止並隱藏
- UIActivityIndicatorView*activityView=(UIActivityIndicatorView*)[self.viewviewWithTag:3000];
- [activityViewstopAnimating];
-
- //加載成功,獲取當前網頁的高度
- CGFloatheight=[[webViewstringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"]floatValue];
- NSLog(@"documentHeight=%@",@(height));
- }
-
- -(void)webView:(UIWebView*)webViewdidFailLoadWithError:(nullableNSError*)error
- {
- NSLog(@"加載失敗");
-
- //加載失敗,不可用
- UISegmentedControl*segmentedControl=(UISegmentedControl*)[self.viewviewWithTag:2000];
- segmentedControl.userInteractionEnabled=NO;
-
- //加載失敗,停止並隱藏
- UIActivityIndicatorView*activityView=(UIActivityIndicatorView*)[self.viewviewWithTag:3000];
- [activityViewstopAnimating];
-
- //加載失敗,提示
- [[[UIAlertViewalloc]initWithTitle:nilmessage:@"加載失敗"delegate:nilcancelButtonTitle:nilotherButtonTitles:@"OK",nilnil]show];
- }
-
- //UIScrollViewDelegate
- -(void)scrollViewDidScroll:(UIScrollView*)scrollView
- {
- //內容滾動變化間距
- CGFloatoffsetY=scrollView.contentOffset.y;
- NSLog(@"offsetX=%@",@(offsetY));
- }
注意:在最後釋放視圖控制器時,務必對內存進行處理。
- -(void)dealloc
- {
- _webview.delegate=nil;
- [_webviewloadHTMLString:@""baseURL:nil];
- [_webviewstopLoading];
- [_webviewremoveFromSuperview];
- [[NSURLCachesharedURLCache]removeAllCachedResponses];
- [UIApplicationsharedApplication].networkActivityIndicatorVisible=NO;
-
- _webview=nil;
- }