How do I use DocumentGroup with ReferenceFileDocument in SwiftUI?

Asked on 08/15/2024

1 search

To use DocumentGroup with ReferenceFileDocument in SwiftUI, you can follow the guidance provided in the session titled "Evolve your document launch experience" from WWDC 2024. Here's a brief overview of the steps involved:

  1. Define a Document Group Scene: In your SwiftUI app, declare a DocumentGroup scene. This is new to iOS 18 and allows you to specify the document type and the view that displays the open document.

  2. Customize the Launch Experience: You can customize the launch experience by changing the title of the create document button and adding decorative views. Use the accessoryViewBuilder parameter to add custom views like images or other UI elements to the launch screen.

  3. Add Template Support: If you want to create documents from templates, you can add an action button titled "Choose a template." When tapped, SwiftUI calls an async closure where you can return a document created from a template. You can present a template picker view and use a continuation to resume the document creation process after a user selects a template.

For more detailed guidance, you can refer to the session Evolve your document launch experience (02:18) which covers getting started with SwiftUI for document-based apps.