SwiftUI app cycle and opening an app document

I am writing a SwiftUI app cycle macOS app and trying to open a custom app document. I have added the appropriate entries to the Info.plist file and the document does open in my app.

I am using NSApplicationDelegateAdaptor to provide an application delegate.

The application(_ application: NSApplication, open urls: [URL]) app delegate method does get called but urls is an empty array. Furthermore an additional app window opens when opening my document.

My app is not a document based app and my custom document is not associated with a class or an NSDocument thing.

Is this broken in SwiftUI app cycle based apps or am I missing something? Is there a solution?

Replies

Seems like I have found a solution which is to add .handlesExternalEvents(matching: []) to the WindowGroup.

  • THANK YOU for adding this answer, it actually worked! Now the application(_ application: NSApplication, open urls: [URL]) app delegate function gets called with the URL array properly populated. Instead of having nothing, it now contains the full path to the document that was opened in the Finder.

    I don't know how you figured this out and this should be documented in SwiftUI since it's so important.

Add a Comment