日常小知识点

WKWebView 控制台输出 js log

2018-08-13  本文已影响60人  ioser_sk

首先,配置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];
上一篇 下一篇

猜你喜欢

热点阅读