美文网首页
2018-06-22

2018-06-22

作者: 王家小雷 | 来源:发表于2018-06-22 15:24 被阅读6次

客户端与web交互的几种方法

1.web端通知客户端

  • (void)webViewDidFinishLoad:(UIWebView *)webView
    {
    JSContext *contentJS = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    __weak typeof(self)weakSelf =self;
    contentJS[@"console"][@"log"] = ^(JSValue * msg) {
    JSValue *jsValue= msg;
    NSString *str = [jsValue toString];
    NSDictionary *dict = [NSJSONSerialization JSONStrToDictionary:str];

// if (str.length>0) {
//
// }
if (dict.count>0) {
NSString *url=[NSString stringWithFormat:@"%@",dict[@"url"]];
NSString *title=[NSString stringWithFormat:@"%@",dict[@"title"]];
}
NSLog(@"H5 log : %@", msg);
};
contentJS[@"console"][@"warn"] = ^(JSValue * msg) {
NSLog(@"H5 warn : %@", msg);
};
contentJS[@"console"][@"error"] = ^(JSValue * msg) {
NSLog(@"H5 error : %@", msg);
};
contentJS[@"openBIWin"] = ^() {

            NSArray *thisArr = [JSContext currentArguments];
    
            for (JSValue *jsValue in thisArr) {
    
                NSLog(@"=======%@",jsValue);
    
            }
  
    NSLog(@"js调用oc---------The End-------");
};

}

2.客户端回调传给web端

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"utils.callFunc('%@')",stingCallBack]];
[webView stringByEvaluatingJavaScriptFromString:[@"" stringByAppendingFormat:@"webView.retValue="%@";", retValue]];

相关文章

网友评论

      本文标题:2018-06-22

      本文链接:https://www.haomeiwen.com/subject/wivkyftx.html