"Error connecting back to host app" in Safari 13

In Safari 13 (both in macOS Catalina and Safari Technology Preview builds on Mojave), I'm encountering a major issue when having my Safari App Extension communicate with Safari. For example - my app's toolbar popover has an option to enable or disable certain functionality for the extension. In Safari 12.1, I am able to use page?.dispatchMessageToScript to send messages from my App Extension to the injected script to immediately reflect the user's change in the foreground tab. This also works in Safari 13, but only at first launch. After Safari has been running for some time, all further attempts to use this functionality fail silently. The only indication of the error is found by checking the console, where I'm seeing:


Error connecting back to host app: NSCocoaErrorDomain, code: 4099


I actually filed a Radar/feedback about this several months back, but have not heard any updates and have not seen this issue improve since June. I see this was also mentioned in a previous thread, but there was no further response. Is anyone else seeing this, or does anyone at Apple have any clarification regarding what's happening? This significantly impacts some useful features of my app/extension (such as being able to make changes in the core app and have them quickly reflected in Safari), so I'd really like to see this addressed before Safari 13 ships.

Replies

What radar did you file? Do you have the radar number handy? I'd like to see if this still reproduces.

I have similar problem with simple Safari Extension App.

After Safari has been running for some time. On `send()` (button click in popover) 'SFSafariApplication.getAllWindows' stops working and completion handler is never fired. Console shows 'Error connecting back to host app: NSCocoaErrorDomain, code: 4099'.


Edit: also 'SFSafariApplication.openWindow' not working after error


Tested on Safari Technology Preview - Release 91 (Safari 13.1, WebKit 14609.1.3) Mojave


SafariExtensionViewController.swift

    @IBAction func send(_ sender: Any) {
        self.count += 1
        SFSafariApplication.getAllWindows { (windows) in
            for window in windows {
                window.getAllTabs { (tabs) in
                    for tab in tabs {
                        tab.getPagesWithCompletionHandler { (pages) in
                            for page in pages ?? [] {
                                page.dispatchMessageToScript(withName: "test", userInfo: ["count": self.count])
                            }
                        }
                    }
                }
            }
        }
    }


    @IBAction func open(_ sender: Any) {
        SFSafariApplication.openWindow(with: URL.init(string: "https://google.com")!) { (window) in
            NSLog("opened")
        }
    }


script.js

document.addEventListener("DOMContentLoaded", function(event) {
     safari.self.addEventListener("message", function(event) {
        if (event.name === 'test') {
          console.log("Message from background received: ", event.message.count);
        }
     });
});

That's a known issue with Safari Technology Preview. It shouldn't affect Safari 13. Additionally, installing Safari 13 will cause this to work correctly in Safari Technology Preview. Sorry for the inconvenince here!