I have web view application and try To Read All Of The javascript Console.log and clicked event of the web view in my application
I write this code , when button is clicked go to userContentController delegate but when have message nothing happen
let config = WKWebViewConfiguration()
let source = """
document.addEventListener('click', function(){ window.webkit.messageHandlers.iosListener.postMessage('click clack!'); })
document.addEventListener('message', function(e){
window.webkit.messageHandlers.iosListener.postMessage(e.data); })
})
"""
let script = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: false)
config.userContentController.addUserScript(script)
config.userContentController.add(self, name: "iosListener")
How can I read console.log of wkwebview messages swift? in UIwebview I can use
let context = self.webView.value(forKeyPath: "documentView.webView.mainFrame.javaScriptContext") as! JSContext
let logFunction : @convention(block) (String) -> Void =
{
(msg: String) in
NSLog("Console: %@", msg)
}
context.objectForKeyedSubscript("console").setObject(unsafeBitCast(logFunction, to: AnyObject.self), forKeyedSubscript: "log" as NSString)
but in wkwebview it dosent work