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