码迷,mamicode.com
首页 > Web开发 > 详细

webView

时间:2015-04-01 17:07:48      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

   一个小的浏览器。。。。。。。。。有后退,前进,刷新,停止,转到,本地,调js

1 _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height-40)];
2     _webView.delegate = self;
3     [self.view addSubview:_webView];

设置几个按钮,添加点击事件,设tag值0~6

后退:[_webView goBack];

前进:[_webView goForward];

刷新:[_webView reload];

停止:[_webView stopLoading];

转到:①首先判断前缀是否有http

         if(![_urlField.text hasPrefix:@"http"]){

                 _urlField.text = [NSString stringWithFormat:@"http://%@",_urlField.text];

          }

         ②发送请求,就会直接转到相应的网站中

          NSURL *url = [NSURL URLWithString:_urlField.text];

          NSURLRequest *request = [NSURLRequest requestWithURL:url];

          [_webView loadRequest:request];

本地:对于本地的html,首先得找到它的路径,将其变成字符串

         NSString *path = [[NSBundle mainBundle] pahtForResource:@"xml2" ofType:@"html"];

         //html字符串

         NSString *htmlStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

         [_webView loadHTMLString:htmlStr baseURL:nil];

调js:oc调js,已经有封装好的方法了,可以直接用

         [_webView stringByEvaluatingJavaScriptFromString:@"func()"];

每次刷新的时候,_textField中的网址没有显示,需要一个方法来让其刷新地址栏

 1  (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
 2 {
 3     //请求的地址,刷新地址栏
 4     _urlField.text = request.URL.absoluteString;
 5     //分割字符串
 6     NSArray *array = [_urlField.text componentsSeparatedByString:@"://"];
 7     if([array[0] isEqualToString:@"oc"]){
 8         //获取到javaScript想调用的方法名称
 9         SEL  sel = NSSelectorFromString(array[1]);
10         [self performSelector:sel];
11     }
12     return YES;
13 }
14 
15 - (void)ocFunc
16 {
17     NSLog(@"OC........");
18 }

 

webView

标签:

原文地址:http://www.cnblogs.com/Angelone/p/4384174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!