How to detect URL schemes with .handlesExternalEvents and NSAppleEventManager simultaneously

539 views Asked by At

I have a SwiftUI-based Mac app with multiple WindowGroups.

1. Opening different SwiftUI WindowGroups using URL schemes

To open those windows I am using URL schemes (like described here):

WindowGroup {
    // ...
}
.handlesExternalEvents(matching: Set(arrayLiteral: "primaryWindow"))

... and then calling:

NSWorkspace.shared.open(URL(string: "myapp://primaryWindow")!)

☑️ This works just fine!

2. Detecting URLs called from outside the app

I also need to be able to recognise and handle URL schemes called from outside the app, like myapp://somePath?someParameter=1. I found this solution and set an event handler within my AppDelegate:

func applicationWillFinishLaunching(_ notification: Notification) {
    NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(self.handleGetURL(event:reply:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL)) )
}

☑️ This works just fine, too, and my #selector method is called like you would expect.

3. Problem: How to use .handlesExternalEvents and .setEventHandler simultaneously?

Here’s where the problem starts: After calling .setEventHandler my WindowGroups no longer react on called URLs and I remain unable to open new windows.

That somehow makes sense since I’ve registered an event handler in AppDelegate specifically for kAEGetURL but I have no idea how to implement both features simultaneously. Looking forward for your advice!

0

There are 0 answers