iOS js交互只能在特定的页面里,不能设置为全局(个人经验,如果可以,望告知)
ios调用js的方法 包含头文件<JavaScriptCore/JavaScriptCore.h> <UIWebViewDelegate>
js中代码
function getdatafromios(result){
$("#barcodediv").html(""+result);
}
法1:
JSContext*context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
NSString*testJS = [NSString stringWithFormat:@"getdatafromios('%@');",strResult.strScanned];//传参
[context evaluateScript:testJS];
法2;
NSString*testJS = [NSString stringWithFormat:@"getdatafromios('%@');",strResult.strScanned];//传参
[self.webView stringByEvaluatingJavaScriptFromString:testJS];
js调用ios的方法
JSContext*context = [webViewvalueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
context[@"jsScanTest"] = ^(
ScanViewController*SVC = [[ScanViewController alloc]init];
[self presentViewController:SVCanimated:YEScompletion:nil];
);
js中代码
functionshowiOSToast() {
javascript:jsScanTest();
}
实现全局交互:把调用的ios方法放到- (void)webViewDidFinishLoad:(UIWebView*)webView;里面
- (void)webViewDidFinishLoad:(UIWebView*)webView {
JSContext*context = [webViewvalueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
JSObject*test = [JSObject new];
webView2= webView;
context[@"jsScanTest"] = test;
test.delegate=self;
}