Contents

DocumentReader

A type that reads a document’s content from a file.

Declaration

protocol DocumentReader<Snapshot>

Overview

SwiftUI calls your document’s reader(configuration:) method to obtain a DocumentReader, then invokes read(from:progress:) in the background with coordinated file access. The returned snapshot is delivered to apply(snapshot:previous:) on the main actor.

Use FileWrapperDocumentReader for cases that don’t require custom file read logic. Implement a DocumentReader when you need direct URL access for frameworks like Core Graphics, AVFoundation, or PDFKit:

struct ImageReader: DocumentReader {
    @concurrent
    func read(from source: URL, progress: consuming Subprogress)
        async throws -> sending CGImage {
        guard let provider =
            CGDataProvider(url: source as CFURL),
              let image = CGImage(
                  jpegDataProviderSource: provider,
                  decode: nil, shouldInterpolate: true,
                  intent: .defaultIntent
              ) else {
            throw CocoaError(.fileReadCorruptFile)
        }
        return image
    }
}

SwiftUI provides the document’s file URL as the reader’s source.

Topics

Reading a document

See Also

Reading and writing documents