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.
Read the configuration from the environment
DocumentConfigurationis delivered through the\.documentConfigurationenvironment key rather than constructed by you. Declaring@Environment(\.documentConfiguration) private var config: DocumentConfiguration?bindsconfigto the active document's configuration, and any view inside aDocumentGrouppicks it up automatically.Handle the optional value
The environment value is a
DocumentConfiguration?, and it isnilwhen no document context is present — for example a preview or a standalone scene. Becauseconfigis optional, every access in the example goes through optional chaining and supplies a fallback, as withconfig?.fileURL?.lastPathComponent ?? "Untitled".Locate the document with fileURL
The
fileURLproperty 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 withconfig?.fileURL?.lastPathComponent, falling back to"Untitled"so unsaved documents still read sensibly.Gate editing on isEditable
The
isEditableBoolean 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.
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.
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()
}
}