客户端与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]];