zoukankan      html  css  js  c++  java
  • webView

       一个小的浏览器。。。。。。。。。有后退,前进,刷新,停止,转到,本地,调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 }
  • 相关阅读:
    Git 分支使用
    ansible 2.7 API
    zabbix api
    (四)ansible 通过堡垒机访问内网服务器
    C#实体对象出现中文处理乱码的问题
    mysql数据库数据(字段数过大)太多导入不了的解决方法
    MathWorks.MATLAB.NET.Arrays.MWArray”的类型初始值设定项引发异常 解决方法
    hibernate 主键生成方式
    HTN规划 jshop2
    自动驾驶
  • 原文地址:https://www.cnblogs.com/Angelone/p/4384174.html
Copyright © 2011-2022 走看看