<!--
{
  "availability" : [
    "iOS: 27.0.0 -",
    "iPadOS: 27.0.0 -",
    "macCatalyst: 27.0.0 -",
    "macOS: 27.0.0 -",
    "visionOS: 27.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/fileExporter(isPresented:document:contentType:defaultFilename:onCompletion:onCancellation:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE12fileExporter11isPresented8document11contentType15defaultFilename12onCompletion0M12CancellationQrAA7BindingVySbG_qd__Sg07UniformJ11Identifiers6UTTypeVSgSSSgys6ResultOy10Foundation3URLVs5Error_pGcyycSgtAA16WritableDocumentRd__AX6Writer_11DestinationRTd__lF"
  },
  "title" : "fileExporter(isPresented:document:contentType:defaultFilename:onCompletion:onCancellation:)"
}
-->

# fileExporter(isPresented:document:contentType:defaultFilename:onCompletion:onCancellation:)

Presents a system dialog for allowing the user to export a
`WritableDocument` to a file on disk.

```
nonisolated func fileExporter<D>(isPresented: Binding<Bool>, document: D?, contentType: UTType? = nil, defaultFilename: String? = nil, onCompletion: @escaping (Result<URL, any Error>) -> Void, onCancellation: (() -> Void)? = nil) -> some View where D : WritableDocument, D.Writer.Destination == URL
```

## Parameters

`isPresented`

A binding to whether the dialog should be shown.

`document`

The in-memory document to export.

`contentType`

The content type to export to. If not provided,
`WritableDocument.writableContentTypes` are used.

`defaultFilename`

If provided, the default name to use
for the exported file, which the user will have
an opportunity to edit prior to the export.

`onCompletion`

A callback that will be invoked when the operation
has succeeded or failed. The `result` indicates whether
the operation succeeded or failed.

`onCancellation`

A callback that will be invoked
if the user cancels the operation.

## Discussion

In order for the dialog to appear, `document` must be non-nil. When
the operation is finished, `isPresented` will be set to `false`
before `onCompletion` is called. If the user cancels the operation,
`isPresented` will be set to `false` and `onCancellation` will be
called.

Below is an example of a simple implementation of `WritableDocument`.
Instead of `String`, you can use `Data` or any other type as the
snapshot.

```
@MainActor
final class TextDocument: WritableDocument {
    static let writableContentTypes: [UTType] = [.utf8PlainText]

    var text: String = ""

    nonisolated func writer(
        configuration: sending WriteConfiguration
    ) -> sending FileWrapperDocumentWriter<String> {
        FileWrapperDocumentWriter(configuration) { snapshot, _ in
            FileWrapper(regularFileWithContents: Data(snapshot.utf8))
        }
    }

    func snapshot(contentType: UTType) async throws -> String {
        text
    }
}

struct ExportView: View {
    @State private var document = TextDocument()
    @State private var isExporting = false

    var body: some View {
        Button("Export…") { isExporting = true }
            .fileExporter(
                isPresented: $isExporting,
                document: document,
                defaultFilename: "Untitled"
            ) { _ in }
    }
}
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)