UIWebView

来源:互联网 发布:华泰大智慧软件下载 编辑:程序博客网 时间:2024/05/21 09:26

xml2.html

<html>

    <head>

       <h1>我是大标题</h1>

       <h2>我是二标题</h2>

       <hr>

    </head>

    <body>

       <script>

           function func(){

                //document.write("<p>灰常不错</p>");

                alert("天气依然很好!");

            }

           function func2(){

                window.location.href="oc://ocFunc"

            }

        </script>

       <p>今天星期日,晴空万里,雷电交加。</p>

        <ahref="http://www.baidu.com">百度</a>

        <button onclick="func2()">点我</button>

    </body>

</html>



#import "ViewController.h"

@interfaceViewController ()<UIWebViewDelegate>

{

   UIWebView *_webView;

   UITextField *_textField;

}


@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

    //地址栏

    _textField = [[UITextFieldalloc] initWithFrame:CGRectMake(0,20, 320, 20)];

    _textField.borderStyle =UITextBorderStyleRoundedRect;

    [self.viewaddSubview:_textField];

    

    NSArray *array =@[@"转到",@"前进",@"后退",@"刷新",@"停止",@"html",@"js"];

   for (int i =0; i < array.count; i++) {

        UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

        button.frame =CGRectMake(i * (320 / array.count),40, 320/array.count,20);

        [button setTitle:array[i]forState:UIControlStateNormal];

        button.tag = i;

        [button addTarget:selfaction:@selector(buttonClick:)forControlEvents:UIControlEventTouchUpInside];

        [self.viewaddSubview:button];

    }

    

    _webView = [[UIWebViewalloc] initWithFrame:CGRectMake(0,60, 320, 420)];

   _webView.delegate =self;

    [self.viewaddSubview:_webView];

}


-(void)buttonClick:(UIButton *)btn

{

   if (btn.tag ==0) {

       //添加前缀

       if (![_textField.texthasPrefix:@"http"]) {

           _textField.text = [NSStringstringWithFormat:@"http://%@",_textField.text];

        }

       NSURLRequest *reqeust = [NSURLRequestrequestWithURL:[NSURLURLWithString:_textField.text]];

        //webView加载请求

        [_webViewloadRequest:reqeust];

        [_textFieldresignFirstResponder];

    }

   //前进

   if (btn.tag ==1) {

        [_webViewgoForward];

    }

   //后退

   if (btn.tag ==2) {

        [_webViewgoBack];

    }

   //刷新

   if (btn.tag ==3) {

        [_webViewreload];

    }

   //停止

   if (btn.tag ==4) {

        [_webViewstopLoading];

    }

    //加载本地html

   if (btn.tag ==5) {

        NSString *path = [[NSBundlemainBundle] pathForResource:@"xml2"ofType:@"html"];

        NSString *htmlStr = [NSStringstringWithContentsOfFile:path encoding:NSUTF8StringEncodingerror:nil];

        //加载本地html

        [_webViewloadHTMLString:htmlStr baseURL:nil];

    }

   /*

     js调用oc代码:

     oc调用js代码: stringByEvaluatingJavaScriptFromString:

     */

   if (btn.tag ==6) {

        [_webViewstringByEvaluatingJavaScriptFromString:@"func()"];

    }

    //js调用oc

    

}


-(void)ocFunc

{

    NSLog(@"我是大oc");

}


-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

{

    //更新地址

   _textField.text = request.URL.absoluteString;

    //jsoc

   /*

     js发起一个非法地址请求,oc截取到,解析地址,拿到要调用的方法名称,进行方法调用

     http://www.baidu.com

     oc://ocFunc

     */

    NSArray *array = [request.URL.absoluteStringcomponentsSeparatedByString:@"://"];

   if ([array[0]isEqualToString:@"oc"]) {

       SEL sel = NSSelectorFromString(array[1]);

        [selfperformSelector:sel];

    }

    

    returnYES;

}


0 0