正如您所期望的那样,使用
stringByEvaluatingJavaScriptFromString
/将数据从JS传递到Obj C use
JSContext
<!-- save as .html and load into UIWebview -->
<!DOCTYPE html>
<html>
<head>
<script>
function loadData(x){
alert(x);
}
function myMeg(){
passDatatoObjC("success");
}
</script>
</head>
</html>
function returnSomething(x){
loadData(x);
}
function passDatatoObjC(x){
passDatatoObjC(x);
}
#pragma mark - UIWebview dataource
-(void)webViewDidFinishLoad:(UIWebView *)webView{
[self.svgWebMapView stringByEvaluatingJavaScriptFromString:@"returnSomething(10)"];
JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
context[@"passDatatoObjC"] = ^(NSString *msg) {
NSLog(@"%@",msg);
});
}