iOS和JS的交互之在代理方法拦截Url,识别判断

来源:互联网 发布:农村淘宝 app 下载 编辑:程序博客网 时间:2024/06/15 16:17

在代理方法拦截Url,识别判断

这种方法原理很简单,UIWebView的界面响应会调起下面的代理方法

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

在该方法中我们可以识别网页链接中的特殊字段,从而达到JS调起原生方法的目的

在本demo中有一个HTML文件


  



//使用本地的h5文件加载一个网页

    NSString *htmlPath = [[NSBundlemainBundle] pathForResource:@"testWebPage"ofType:@"html"];

    NSError *error =nil;

    NSString  *str = [NSStringstringWithContentsOfFile:htmlPathencoding:NSUTF8StringEncodingerror:&error];

    [self.webViewloadHTMLString:str baseURL:nil];


在代理方法中的处理

#pragma mark UIWebViewDelegate


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

    //获取此时的URL

    //'http://www.testwebpage/?funcName=printInfo:&&info=helloword'

    NSURL *url = [requestURL];

    NSString *completeString = [urlabsoluteString];

    

    //第一步:检测链接中的特殊字段

    NSString *needCheckStr =@"http://www.testwebpage/?";

    NSRange jumpRange = [completeStringrangeOfString:needCheckStr];

    if (jumpRange.location !=NSNotFound) {

        /*

         1.检测到链接中包含有特殊字段,客户端要接受响应并做后续处理

         这就相当于js调起了iOS

         2.在真实的使用时,客户端需要和h5协调,双方需要统一监听的字段

         3.参数问题:如果此时的交互需要传递参数,参数也可以放在链接里,

         同样通过识别字符串的方法来获取

         */

        

        //第二步:拿到链接字符串的后续部分,然后分割字符串得到参数数据

        NSMutableString *linkmStr = [NSMutableStringstringWithString:completeString];

        NSRange deleteRange = {0,needCheckStr.length};

        [linkmStr deleteCharactersInRange:deleteRange];

        NSArray *params = [linkmStrcomponentsSeparatedByString:@"&&"];

        //取出第一个参数:与h5协商好的方法名

        NSString *funcName = [params[0]componentsSeparatedByString:@"="][1];

        //取出第二个参数:信息字符串

        NSString *info = [params[1]componentsSeparatedByString:@"="][1];

        

        //第三步:调起iOS原生方法

        SEL ocFunc =NSSelectorFromString(funcName);

        if ([selfrespondsToSelector:ocFunc]) {

           //使用编译预处理,不显示警告提示

#pragma clang diagnostic push

#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

            [selfperformSelector:ocFunc withObject:info];

#pragma clang diagnostic pop

            

        }

        //返回NO是为了不再执行点击原链接的跳转

        returnNO;

    }

    returnYES;

    

}

事件的点击处理

#pragma mark - 事件点击

- (void)printInfo:(NSObject *)obj{

    _info = [NSStringstringWithFormat:@"%@",obj];

    NSLog(@"打印JS传递的info:%@",_info

          );

    UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"提示"message:[NSStringstringWithFormat:@"要拨打电话\n%@",_info]delegate:selfcancelButtonTitle:@"点错了!!"otherButtonTitles:@"确定",nil];

    [alertView show];

    

}


#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    

    if (buttonIndex ==1) {

        [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:[NSStringstringWithFormat:@"tel://%@",_info]]];

    }

    

}

demo的下载地址:http://download.csdn.net/detail/iosbird/9594106

1 0