Two document types in SwiftUI?

Documentation of DocumentGroup says:

Your app can support multiple document types by adding additional document group scenes:

The example has an editor and a viewer, but I need to have two editors. Creating two different DocumentGroup elements seems to work, but then there's only one New Document menu item, which targets the first DocumentGroup.

Is there a way to allow the user to choose which document type to create?

Replies

It looks like the answer is no.

The second DocumentGroup is necessary for opening a file of the second type, but I haven't found a way to make SwiftUI create a document of that type. Using .handlesExternalEvents doesn't seem to help, as it seems to want to open a window defined by a WindowGroup rather than a DocumentGroup.

The best idea I've come up with is to create a temporary file and then push the URL for that to openURL, but that leaves the user with a file which they don't actually need, and doesn't replicate the experience of a new document.

  • Just a correction. It's not openURL but NSWorkspace.shared.open that opens the document. It works fine, but, as I said, it's now bound to a file that is not where the user wants (the app is sandboxed, so it's hiding inside the container in the Library folder).

Add a Comment

This was the best answer that I found: https://stackoverflow.com/a/71978849/12299030