Contents

init(viewing:viewer:)

Creates a document group capable of viewing file documents.

Declaration

nonisolated init(viewing documentType: Document.Type, @ContentBuilder viewer: @escaping (FileDocumentConfiguration<Document>) -> Content)

Parameters

  • documentType:

    The type of document your app can view.

  • viewer:

    The viewing UI for the provided document.

Discussion

Use this method to create a document group that can view files of a specific type. The example below creates a new document viewer for MyImageFormatDocument and displays them with MyImageFormatViewer:

@main
struct MyApp: App {
    var body: some Scene {
        DocumentGroup(viewing: MyImageFormatDocument.self) { file in
            MyImageFormatViewer(image: file.document)
        }
    }
}

With the viewing: initializer, SwiftUI considers your app as a viewer for given content types (readable content types declared on the document type). No File > New menu item is added on macOS, no New Document button appears in the iOS document browser, and the isEditable property is false, preventing accidental writes. Use the init(newDocument:editor:) initializer instead if your app needs to create or edit documents.

You tell the system about the app’s role with respect to the document type by setting the CFBundleTypeRole Info.plist key with a value of Viewer.

See Also

Creating a document group