首先,配置webview的configuration,注册回调
#define kCustomJSLoggerName @"Consol_logger"
WKWebViewConfiguration *_configuration = webView.configuration;
[_configuration.userContentController addScriptMessageHandler:self name:kCustomJSBridgeName];
然后,实现回调方法
userContentController: didReceiveScriptMessage:
if ([message.name isEqualToString:kCustomJSLoggerName]) {
NSLog(@"message = %@",message.body);
}
最后的最后,别忘了在析构函数中取消回调
[_configuration.userContentController removeScriptMessageHandlerForName:kCustomJSLoggerName];
网友评论