TechnologiesSwiftUIDocuments

DocumentCreationContext struct

iOSmacOStvOSwatchOSvisionOS✓ renders

Provides context about how a document was created or opened.

How it works

A DocumentCreationContext carries information about a document that SwiftUI is about to create, handing it to your scene at the moment a new file is requested. You receive it inside a DocumentGroup's new-document closure, where it lets your initialization logic adapt to how and why the document was started rather than always producing an identical blank file. Reach for it when the same document type should open differently depending on its origin, so the context becomes the single place to read that intent and seed the model accordingly.

  1. Receive the context from DocumentGroup's new-document closure

    DocumentCreationContext is not something you construct yourself; SwiftUI builds it and passes it to the closure you supply when configuring a DocumentGroup for new documents. That closure is the hook where the system reports that a fresh file is being made, mirroring the Create Document button action in the example that signals the same intent to start a new document.

  2. Read the context to branch your initialization

    Inside the closure, inspect the DocumentCreationContext value to decide how to populate the new document instead of returning a fixed default. This is the structure's whole job: turning one creation entry point into branching setup logic, the work the comment in the example points at where it notes the newDocument closure receives a DocumentCreationContext describing the new file.

  3. Return a configured document model

    After consulting the context, the closure produces and returns the document instance that backs the editor scene. The DocumentCreationContext influences that returned value rather than the view tree itself, so state such as the example's newDocs counter conceptually stands in for the per-creation bookkeeping you would update as each contextualized document is built.

  4. Keep the context scoped to creation only

    A DocumentCreationContext is meaningful only during the creation moment and is not retained as document state, which is why the view body in DocumentCreationContextDemo never references it directly. The Image, Text, and Button here are ordinary scene chrome; the symbol lives upstream in the document-group plumbing that drives them.

Try it — Change the Create Document button's newDocs += 1 action to set distinct values based on a condition, mirroring how reading the DocumentCreationContext would let the new-document closure seed different starting content per creation.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

DocumentCreationContext.swift
struct DocumentCreationContextDemo: View {
    @State private var newDocs = 0

    var body: some View {
        VStack(spacing: 16) {
            Image(systemName: "doc.badge.plus")
                .font(.system(size: 44))
                .foregroundStyle(.tint)
            Text("New Documents: \(newDocs)")
                .font(.headline)
            Button("Create Document") {
                // In a DocumentGroup, the newDocument closure receives
                // a DocumentCreationContext describing the new file.
                newDocs += 1
            }
            .buttonStyle(.borderedProminent)
        }
        .padding()
    }
}
Live preview
New Documents: 0 Create Document
New Documents: 0 Create Document
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →