0

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!

ixany
  • 5,433
  • 9
  • 41
  • 65
  • 1
    Why `onOpenURL` does not work for you? – Asperi Dec 25 '21 at 17:25
  • @Asperi Because I have to stick `.onOpenURL` to a specific View (like `.handlesExternalEvents`) and want to receive and handle URL scheme events regardlessly of that. My approach within AppDelegate with `.setEventHandler` seems to overwrite the `handlesExternalEvents` modifier. Am I missing something here? How would you open a WindowGroup within `onOpenURL`? – ixany Dec 25 '21 at 18:42

0 Answers0