UIWebView的用法


(1)創建

UIWebView  *myWebView=[[ UIWebView allocinitWithFrame:CGRectMake(020320300)];
 (2)加載網頁

     NSURL *url=[NSURLURLWithString:@"http://www.google.com.hk"];

     NSURLRequest *request=[[NSURLRequestallocinitWithURL:url];

     [ myWebView  loadRequest:request]; 
(3)是否與用戶交互(即用戶能不能控制webview)
     [ myWebView  setUserInteractionEnabled:YES];  
(4)顯示 UIWebView
     [ self . view  addSubview:myWebView];

(5)導航

     [webView goBack];//返回

 [webView goForward];//向前
 [webView reload];//重新加載數據
     [webView stopLoading];//停止加載數據

(6)委托

     1.-(BOOL)webView:(UIWebView *)webView  shouldStartLoadWithRequest:(NSURLRequest *)request

          navigationType:(UIWebViewNavigationType)navigationType;

web視圖指示加載內容時通知。應該返回YES開始加載。導航提供的類型參數,是指請求的來源,可以是下列任何一個:
     UIWebViewNavigationTypeLinkClicked
     UIWebViewNavigationTypeFormSubmitted
     UIWebViewNavigationTypeBackForward
     UIWebViewNavigationTypeReload
     UIWebViewNavigationTypeFormResubmitted
     UIWebViewNavigationTypeOther

     2.-(void)webViewDidStartLoad:(UIWebView *)webView;//當開始請求的時候被通知
     3.-(void)webViewDidFinishLoad:(UIWebView *)webView;//當結束請求的時候被通知
     4.-(void)webView:(UIWebView *)webView  didFailLoadWithError:(NSError *)error;//當請求中出現錯誤時被通知

(7)UIWebView和JS交互

     1》在Objective-C代碼中調用JS
 
     使用 stringByEvaluatingJavaScriptFromString 方法,需要等到UIWebView中的頁面加載完成之后去調用。

     -(void) webViewDidFinishLoad:(UIWebView *)webView{

         [self.activityViewstopAnimating];

 

         [myWebView stringByEvaluatingJavaScriptFromString:@"function test(){ alert(123123123)}"];

         [myWebView stringByEvaluatingJavaScriptFromString:@"test();"];//調用

     }
     2》在JS中調用Objective-C代碼
 
JS代碼:
  1. function sendCommand(cmd,param){  
  2.     var url="testapp:"+cmd+":"+param;  
  3.     document.location = url;  
  4. }  
  5. function clickLink(){  
  6.     sendCommand("alert","你好嗎?");  
  7. }  
 
Objective-C代碼:
  1. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {  
  2.       
  3.     NSString *requestString = [[request URL] absoluteString];  
  4.     NSArray *components = [requestString componentsSeparatedByString:@":"];  
  5.     if ([components count] > 1 && [(NSString *)[components objectAtIndex:0] isEqualToString:@"testapp"]) {  
  6.         if([(NSString *)[components objectAtIndex:1] isEqualToString:@"alert"])   
  7.         {  
  8.             UIAlertView *alert = [[UIAlertView alloc]   
  9.                                   initWithTitle:@"Alert from Cocoa Touch" message:[components objectAtIndex:2]  
  10.                                   delegate:self cancelButtonTitle:nil  
  11.                                   otherButtonTitles:@"OK", nil];  
  12.             [alert show];  
  13.         }  
  14.         return NO;  
  15.     }  
  16.     return YES;  
  17. }  


免責聲明!

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



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