Bug iOS 17.2 Beta only webView.configuration.userController.add(_ scriptMessageHandler: WKScriptMessageHandler, name: String) Not Working

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')

Post not yet marked as solved Up vote post of artuAtPandora Down vote post of artuAtPandora
1.2k views

Replies

I tried removing the custom handlers and attempted this:

(function() {
            addEventListener('mousedown', function() {
                window.webkit.messageHandlers.postMessage('WKUserScriptMouseDown');
            }, true);
        })();

I would think this broadcast the message to all the handlers?? However now I get the error message:

TypeError: undefined is not an object (evaluating 'window.webkit.messageHandlers.postMessage')

My next attempt is to wrap this line into a (function() {})(); but if someone else has an idea or is facing similar issues with iOS beta 17.2 only it would be great to hear ideas

I tried some other technique to debug this and get to the root of the issue, I decided to put window.webkit.messageHandlers.handlerOne.postMessage(... and window.webkit.messageHandlers.handlerTwo.postMessage(... inside an if conditional and check which is the object that is `undefined.

(function() {
            addEventListener('mousedown', function() {
              if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.handlerOne) {
                   window.webkit.messageHandlers.handlerOne.postMessage('WKUserScriptMouseDown');
              }
            }, true);
        })();

After debugging this I found out that window.webkit.messageHandlers is the undefined object, which leads me to believe that webView.configuration.userContentController.add(handlerClass, name: "...") is broken on iOS 17.2 beta

hence messageHandlers is never defined, I'd appreciate to hear if this is happening to someone else with their apps.

Not sure if this is realated but I've had all kinds of Webview/Webkit issues in the iOS 17.2 Beta "21C5029g". Regarless of browser, I get a breif site offline error with almost every website I go to, but then they load. I've reset network settings. Did not help. The bigger inconvience is the beta broke the fuctionality of my gradio webui I use for GenAI stuff like Stable Diffusion (Automatic-1111). The WebUI functions perfectly in every other browser and device, except my iPhone 14 Pro Max with the current beta.

I have same issues in the ios 17.1 - iphone 12