TechnologiesSwiftUIDocuments

DocumentConfiguration struct

iOSmacOStvOSwatchOSvisionOS✓ renders

The configuration of a document in a ``DocumentGroup``.

How it works

DocumentConfiguration carries the runtime details of the document a scene is currently editing — where it lives on disk and whether it can be changed. SwiftUI populates it for views inside a DocumentGroup, so instead of threading file metadata down through your view hierarchy you read it from the environment at the point you need it. Reach for it whenever a document-based app's UI has to react to the current file: showing its name in a header, disabling controls for a read-only file, or branching on whether a backing URL exists yet.

  1. Read the configuration from the environment

    DocumentConfiguration is delivered through the \.documentConfiguration environment key rather than constructed by you. Declaring @Environment(\.documentConfiguration) private var config: DocumentConfiguration? binds config to the active document's configuration, and any view inside a DocumentGroup picks it up automatically.

  2. Handle the optional value

    The environment value is a DocumentConfiguration?, and it is nil when no document context is present — for example a preview or a standalone scene. Because config is optional, every access in the example goes through optional chaining and supplies a fallback, as with config?.fileURL?.lastPathComponent ?? "Untitled".

  3. Locate the document with fileURL

    The fileURL property is the file-system URL backing the document, and it too is optional because a brand-new, never-saved document has no URL yet. The example derives a display title from it with config?.fileURL?.lastPathComponent, falling back to "Untitled" so unsaved documents still read sensibly.

  4. Gate editing on isEditable

    The isEditable Boolean reports whether SwiftUI considers the current document writable, reflecting file permissions and the document's open mode. The example drives both an icon and a label from it — (config?.isEditable ?? false) ? "pencil" : "lock" and the matching "Editable" / "Read Only" text — so the interface mirrors the document's actual editability.

Try it — Force the locked branch by changing config?.isEditable ?? false to config?.isEditable ?? true, then open a read-only file: the icon and caption should flip from lock / Read Only back to pencil / Editable, showing how isEditable decides the rendered state.

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.

DocumentConfiguration.swift
struct DocumentConfigurationDemo: View {
    @Environment(\.documentConfiguration) private var config: DocumentConfiguration?

    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            Label("Document", systemImage: "doc.text")
                .font(.headline)
            Text(config?.fileURL?.lastPathComponent ?? "Untitled")
                .font(.subheadline)
                .foregroundStyle(.secondary)
            HStack {
                Image(systemName: (config?.isEditable ?? false) ? "pencil" : "lock")
                Text((config?.isEditable ?? false) ? "Editable" : "Read Only")
            }
            .font(.callout)
        }
        .padding()
    }
}
Live preview
Document Untitled Read Only
Document Untitled Read Only
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →