I have the following lines of code in our app:
@objc convenience init(webView: WKWebView, delegate: SomeDelegate, navigationDelegate: WKNavigationDelegate) {
self.webView = webView
self.webView.makeInspectable()
self.webView.navigationDelegate = navigationDelegate
self.webView.uiDelegate = self
self.webView.configuration.suppressesIncrementalRendering = true
self.webView.configuration.userContentController.add(weakSelf ?? self,
name: "handlerOne")
self.webView.configuration.userContentController.add(weakSelf ?? self,
name: "handlerTwo")
injectUserScripts() // Injects WKUserScripts into the webView.configuration.userContentController
}
My JS script looks as follows:
(function() {
addEventListener('mousedown', function() {
window.webkit.messageHandlers.handlerOne.postMessage('WKUserScriptMouseDown');
}, true);
})();
if (MyApp.getProperty("os") != null) {
window.webkit.messageHandlers.handlerTwo.postMessage(url);
} else {
console.log(url);
// non-mobile browser (for testing)
}
However I keep seeing an error in my Safari debugger:
TypeError: undefined is not an object (evaluating 'window.webkit.messageHandlers.handlerOne')
TypeError: undefined is not an object (evaluating 'window.webkit.messageHandlers.handlerTwo')