UIWebView的使用方法

来源:互联网 发布:电信云计算发展前景 编辑:程序博客网 时间:2024/05/12 15:14

UIWebView的使用方法

[objc] view plain copy
  1. //1.创建、设置代理  
  2.     UIWebView *webView=[[UIWebView alloc] initWithFrame:CGRectMake(020320300)];  
  3.     webView.delegate = self;  
  4. //2.加载网页  
  5.     NSURL *url=[NSURL URLWithString:@"http://www.google.com.hk"];  
  6.     NSURLRequest *request=[[NSURLRequest alloc] initWithURL:url];  
  7.     [webView loadRequest:request];  
  8. //3.加载本地资源  
  9. NSURL* url = [NSURL fileURLWithPath:filePath];  
  10.     NSURLRequest* request = [NSURLRequest requestWithURL:url];  
  11.     [webView loadRequest:request];  
  12. //4.是否与用户交互(即用户能不能控制webview)  
  13.     [webView setUserInteractionEnabled:YES];  
  14. //5.显示 UIWebView  
  15.     [self.view addSubview:webView];  
  16. //6.导航  
  17.     [webView goBack];//返回  
  18.     [webView goForward];//向前  
  19.     [webView reload];//重新加载数据  
  20.     [webView stopLoading];//停止加载数据  
  21. //7.自动对页面进行缩放以适应屏幕  
  22.     webView.scalesPageToFit = YES;  
  23. //8.自动检测网页上的电话号码,单击可以拨打  
  24.     webView.detectsPhoneNumbers = YES;  
  25. //9.UIWebView 还支持将一个NSString对象作为源来加载。你可以为其提供一个基础URL,来指导UIWebView对象如何跟随链接和加载远程资源  
  26.     [webView loadHTMLString:myHTML baseURL:[NSURL URLWithString:@"http://baidu.com"]];  
  27. //10.UIWebView和JS交互  
  28. //(1)在Objective-C代码中调用JS  
  29. //使用stringByEvaluatingJavaScriptFromString方法,需要等到UIWebView中的页面加载完成之后去调用。  
  30. -(void) webViewDidFinishLoad:(UIWebView *)webView{  
  31.     [self.activityViewstopAnimating];  
  32.     [myWebView stringByEvaluatingJavaScriptFromString:@"function test(){ alert(123123123)}"];  
  33.     [myWebView stringByEvaluatingJavaScriptFromString:@"test();"];//调用  
  34. }  
  35. //(2)在JS中调用Objective-C代码  
  36.     //JS代码:  
  37.     function sendCommand(cmd,param){  
  38.         var url="testapp:"+cmd+":"+param;  
  39.         document.location = url;  
  40.     }  
  41.     function clickLink(){  
  42.         sendCommand("alert","你好吗?");  
  43.     }  
  44.     //Objective-C代码:  
  45. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {  
  46.     NSString *requestString = [[request URL] absoluteString];  
  47.     NSArray *components = [requestString componentsSeparatedByString:@":"];  
  48.     if ([components count] > 1 && [(NSString *)[components objectAtIndex:0] isEqualToString:@"testapp"]) {  
  49.         if([(NSString *)[components objectAtIndex:1] isEqualToString:@"alert"])  
  50.         {  
  51.             UIAlertView *alert = [[UIAlertView alloc]  
  52.                                   initWithTitle:@"Alert from Cocoa Touch" message:[components objectAtIndex:2]  
  53.                                   delegate:self cancelButtonTitle:nil  
  54.                                   otherButtonTitles:@"OK", nil nil];  
  55.             [alert show];  
  56.         }  
  57.         return NO;  
  58.     }  
  59.     return YES;  
  60. }  
UIWebView的委托方法
//1.web视图指示加载内容时通知。应该返回YES开始加载。导航提供的类型参数,是指请求的来源,可以是下列任何一个:  //UIWebViewNavigationTypeLinkClicked     用户触击了一个链接  //UIWebViewNavigationTypeFormSubmitted   用户提交了一个表单  //UIWebViewNavigationTypeBackForward     用户触击前进或返回按钮  //UIWebViewNavigationTypeReload          用户触击重新加载的按钮  //UIWebViewNavigationTypeFormResubmitted 用户重复提交表单  //UIWebViewNavigationTypeOther           发生其它行为  -(BOOL)webView:(UIWebView *)webView  shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;    //2.开始加载的时候执行该方法。  - (void)webViewDidStartLoad:(UIWebView *)webView;    //3.加载完成的时候执行该方法。  - (void)webViewDidFinishLoad:(UIWebView *)webView;    //4.加载出错的时候执行该方法。  - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
    /*------------------------webView加载本地html-------------------*/          //获取数据          NSDictionary *json = [DataReceive requestData:news_detail];          NSString *content = json[@"content"];   //内容          NSString *title =   json[@"title"];     //标题          NSString *time =    json[@"time"];      //时间          NSString *source =  json[@"source"];    //来源          NSString *author =  json[@"author"];    //作者                    //加载html          NSString *filePath = [[NSBundle mainBundle]pathForResource:@"news" ofType:@"html"];          NSString *html = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];          //依次放入数据          NSString *htmlString = [NSString stringWithFormat:html,title,source,time,content,author];          //webView加载          [_webView loadHTMLString:htmlString baseURL:nil];  


1.在获取数据中,顾名思义,获取数据;
2.加载html,需要获取他的本地路径,然后进行编码处理;
3.然后根据格式依次放入数据;


虽然这里是加载本地HTML,其实是借用了这个本地HTML的格式,你也可以使用网络请求下的数据来填充,都一样



0 0