UIWebView的使用


1 UIWebView *wv = [[UIWebView alloc] init]; // 初始化瀏覽器控件UIWebView
2 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.dbw.cn"]]; // 定義請求地址
3 [wv loadRequest:request]; // 利用瀏覽器訪問地址

1、使用UIWebView加載網頁

運行XCode 4.3,新建一個Single View Application,命名為WebViewDemo。

2、加載WebView

在ViewController.h添加WebView成員變量和在ViewController.m添加實現

 

[cpp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4. {  
  5.     UIWebView *webView;  
  6. }  
  7. @end  
[cpp]  view plain copy
  1. ViewController.m  
[cpp]  view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];  
  5.     NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];  
  6.     [self.view addSubview: webView];  
  7.     [webView loadRequest:request];  
  8. }  

運行,這樣百度網頁就打開了

 

 

手機的網絡環境是實時變化的,網絡慢的時候,怎么提示用戶網頁正在打開呢?在網頁打開出錯的時候怎么提示用戶呢?這時候我們就需要知道網頁什么時候打開的,

什么時候加載完成,什么時候出錯了。那么我們需要實現這個<UIWebViewDelegate>協議

3、實現協議,在ViewController.h修改如下:

 

[cpp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController<UIWebViewDelegate>  
  4. {  
  5.     UIWebView *webView;  
  6. }  
  7. @end  


按住control+command+向上鍵,切換到ViewController.m文件,這是我們在文件中打入- (void) webView,就能看到如下實現方法:

 

 

 

UIWebView中幾個重要的函數
1.- (void )webViewDidStartLoad:(UIWebView  *)webView   網頁開始加載的時候調用
2.- (void )webViewDidFinishLoad:(UIWebView  *)webView  網頁加載完成的時候調用
3.- (void)webView:(UIWebView *)webView  didFailLoadWithError:(NSError *)error 網頁加載錯誤的時候調用

4、實現這三個方法,加入NSLog。

先在viewDidLoad 的webView實例化下面加上

    [webView setDelegate:self];設置代理。這樣上面的三個方法才能得到回調。

三個方法實現如下:

 

[cpp]  view plain copy
  1. <span style="font-family:Arial, Verdana, sans-serif;color:#333333;">- (void) webViewDidStartLoad:(UIWebView *)webView  
  2. {  
  3.     NSLog(@"webViewDidStartLoad");  
  4. }  
  5. - (void) webViewDidFinishLoad:(UIWebView *)webView  
  6. {  
  7.     NSLog(@"webViewDidFinishLoad");  
  8. }  
  9. - (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error  
  10. {  
  11.     NSLog(@"didFailLoadWithError:%@", error);  
  12. }  
  13. </span>  

運行打印:

2012-06-23 15:20:29.728 WebViewDemo[1001:f803] webViewDidStartLoad

2012-06-23 15:20:29.991 WebViewDemo[1001:f803] webViewDidFinishLoad

那我們試試error情況,把wifi關掉,運行打印結果:

 

 

2012-06-23 15:23:58.939 WebViewDemo[1087:f803] webViewDidStartLoad

2012-06-23 15:23:59.016 WebViewDemo[1087:f803] webViewDidFinishLoad

請求結果不變,為什么關掉網絡還成功了呢?緩存?我換163.com試試,這是真正的結果出來了:

 

2012-06-23 15:24:41.131 WebViewDemo[1134:f803] webViewDidStartLoad

2012-06-23 15:24:41.149 WebViewDemo[1134:f803] didFailLoadWithError:Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo=0x6b41660 {NSErrorFailingURLStringKey=http://www.163.com/, NSErrorFailingURLKey=http://www.163.com/, NSLocalizedDescription=The Internet connection appears to be offline., NSUnderlyingError=0x6eae690 "The Internet connection appears to be offline."}

連接錯誤了,調用了didFailLoadWithError。

 

5、加載等待界面

為了給用戶更直觀的界面效果,我們加上等待的loading界面試試

 

在webViewDidStartLoad加入等待

 

 

 

[cpp]  view plain copy
  1. <strong>- (void) webViewDidStartLoad:(UIWebView *)webView  
  2. {  
  3.     //創建UIActivityIndicatorView背底半透明View       
  4.     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];    
  5.     [view setTag:108];    
  6.     [view setBackgroundColor:[UIColor blackColor]];    
  7.     [view setAlpha:0.5];    
  8.     [self.view addSubview:view];    
  9.       
  10.     activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];    
  11.     [activityIndicator setCenter:view.center];    
  12.     [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];    
  13.     [view addSubview:activityIndicator];    
  14.   
  15.     [activityIndicator startAnimating];  
  16.    </strong>  


加載完成或失敗時,去掉loading效果

 

 

 

 

[cpp]  view plain copy
  1. <strong>- (void) webViewDidFinishLoad:(UIWebView *)webView  
  2. {  
  3.     [activityIndicator stopAnimating];  
  4.     UIView *view = (UIView*)[self.view viewWithTag:108];  
  5.     [view removeFromSuperview];  
  6.     NSLog(@"webViewDidFinishLoad");  
  7.   
  8. }  
  9. - (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error  
  10. {  
  11.     [activityIndicator stopAnimating];  
  12.     UIView *view = (UIView*)[self.view viewWithTag:108];  
  13.     [view removeFromSuperview];  
  14.     </strong>  

運行效果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM