How it works
URLDocumentConfiguration describes a document that SwiftUI manages by file URL, surfacing the two facts a document view most often needs: where the document lives on disk and whether the user is allowed to change it. SwiftUI hands you a value of this type inside a DocumentGroup scene so the view body can adapt to the document being opened, rather than threading a path and an editability flag through your own state. Reach for it when a document-based app needs to read the backing file location or branch its UI between an editable and a read-only presentation.
Read the backing location from fileURL
The fileURL property exposes the document's location as an optional URL, which is nil for a brand-new document that has never been saved. The example mirrors this shape with documentURL, deriving a display name from it via
documentURL?.lastPathComponentand falling back to"Untitled"when there is no URL yet.Branch on the isEditable flag
isEditable reports whether SwiftUI considers the document writable, letting the view present editing affordances only when changes will actually take effect. The example models this with the
isEditableBoolean, switching the statusTextbetween"Editable"and"Read-only"and tinting it withforegroundStyleso the document's mutability is visible at a glance.Receive the configuration inside DocumentGroup
You don't construct URLDocumentConfiguration yourself; SwiftUI supplies it to the editor content of a DocumentGroup scene, where it stands in for the document currently being shown. The demo's
URLDocumentConfigurationDemoview is the kind of body that plugs into that slot, building aLabeland document summary from the configuration's values.Drive document-aware presentation
Because the configuration carries both location and editability together, the same view can render a title from the file name and gate interactive controls on writability without separate plumbing. Here the
VStackcomposesdocumentURL?.lastPathComponentand theisEditablestatus into one coherent document header.
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 URLDocumentConfigurationDemo: View {
let documentURL: URL? = URL(string: "file:///Notes/todo.txt")
let isEditable = true
var body: some View {
VStack(alignment: .leading, spacing: 12) {
Label("Document", systemImage: "doc.text")
.font(.headline)
Text(documentURL?.lastPathComponent ?? "Untitled")
.font(.body.monospaced())
Text(isEditable ? "Editable" : "Read-only")
.font(.caption)
.foregroundStyle(isEditable ? .green : .secondary)
}
.padding()
}
}