TechnologiesSwiftUI

URLDocumentConfiguration class

iOSmacOStvOSwatchOSvisionOS✓ renders

A set of settings and properties of an open document.

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.

  1. 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?.lastPathComponent and falling back to "Untitled" when there is no URL yet.

  2. 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 isEditable Boolean, switching the status Text between "Editable" and "Read-only" and tinting it with foregroundStyle so the document's mutability is visible at a glance.

  3. 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 URLDocumentConfigurationDemo view is the kind of body that plugs into that slot, building a Label and document summary from the configuration's values.

  4. 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 VStack composes documentURL?.lastPathComponent and the isEditable status into one coherent document header.

Try it — Set documentURL to nil and watch the header fall back to "Untitled", showing how URLDocumentConfiguration's absent fileURL signals an unsaved document.

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.

URLDocumentConfiguration.swift
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()
    }
}
Live preview
Document Untitled Editable
Document Untitled Editable
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →