ios html5页面返回按钮点击事件监听,IOS OC向h5注入JS代码,知道h5的页面按钮ID,向该按钮添加点击事件...
欢迎使用Markdown编辑器写博客本Markdown编辑器使用[StackEdit][6]修改而来,用它写博客,将会带来全新的体验哦:使用WKwebVIew,创建这个对象,代码如下** WKUserScript *script = [[WKUserScript alloc]initWithSource:@”” injectionTime:WKUserScriptInjectionTimeAtDo
欢迎使用Markdown编辑器写博客
本Markdown编辑器使用[StackEdit][6]修改而来,用它写博客,将会带来全新的体验哦:
使用WKwebVIew,创建这个对象,代码如下
** WKUserScript *script = [[WKUserScript alloc]initWithSource:@”” injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
[config.userContentController addUserScript:script];
WKWebView *web = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) configuration:config];
web.backgroundColor = [UIColor yellowColor];
web.navigationDelegate = self;
web.UIDelegate = self;
self.webView = web;
[self.view addSubview:web];
NSString *filrStr = [[NSBundle mainBundle] pathForResource:@”js” ofType:@”html”];
NSURL *url = [NSURL fileURLWithPath:filrStr];
[web loadFileURL:url allowingReadAccessToURL:url];
[[web configuration].userContentController addScriptMessageHandler:self name:@”jsCallOC”];**
在网页加载完成后调用这个回调函数,并在里面注入JS代码
/* 网页加载完成/
-(void)webView:(WKWebView )webView didFinishNavigation:(WKNavigation)navigation{
// NSString *butValueJS = @”document.getElementById(‘buttonJS’).innerText”;
NSString *butValueJS =
@”var button = document.getElementById(‘buttonJS’);button.οnclick=function getImages(){window.webkit.messageHandlers.jsCallOC.postMessage({body: ‘goodsId=1212’});}”;
[webView evaluateJavaScript:butValueJS completionHandler:^(id _Nullable response, NSError * _Nullable error) {
}];
}
下面是截图
/* 接收JS中方法执行事件。/
-(void)userContentController:(WKUserContentController )userContentController didReceiveScriptMessage:(WKScriptMessage)message{
NSLog(@”message.body == %@”,message.name);
[self JCtoOC:message];
}
-(void)JCtoOC:(id)body{
NSLog(@”js调用我的..”);
}
最后在这里接收JS执行的事件
更多推荐
所有评论(0)