https://www.jianshu.com/p/e74bc7abac8d
oc to js
NSString *str = [webView stringByEvaluatingJavaScriptFromString:@"show('sssff')"];
// 或
JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
NSString *textJS = @"show('sssff')";
[context evaluateScript:textJS];
js to oc
#import <JavaScriptCore/JavaScriptCore.h>
JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
context[@"scan"] = ^() {
NSLog(@"scan");
};
html js
<!DOCTYPE html>
<head>
<script type="text/javascript">
function show(str){
return str;
}
function calloc(){
//window.location.href= "vv://vv.com"
scan()
}
</script>
</head>
<body>
<input type="button" value="JS调用OC方法" onclick="calloc()"/>
</body>
</html>