1. 添加JavaScriptCore.framework
Build Phases --> Link Binary With Libraries --> + --> 搜索JavaScriptCore.framework --> 添加
2. 在要调用的含有WebVIew的controller里面导入头文件
3. 在webView的代理方法里面进行编写
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (self.ccTag == 200) {
// 获取JS的上下文
JSContext *context = [self.alertWebView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//定义好JS要调用的方法, hide就是调用的hide方法名
context[@"hide"] = ^() {
NSArray *args = [JSContext currentArguments];
dispatch_async(dispatch_get_main_queue(), ^{
// OC的方法
[self.tvuCallListView hideAllViewsAboutCallListView];
});
for (JSValue *jsVal in args) {
NSLog(@"%@", jsVal.toString);
}
};
}
下面会粘贴处对应的JS端的写法
<html>
<header>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function secondClick() {
hide();
}
</script>
</header>
<body>
<h2> 这里是第二种方式 </h2>
<br/>
<br/>
<button type="button" onclick="secondClick()">Click Me!</button>
</body>
</html>