fileExporter(isPresented:items:contentTypes:onCompletion:onCancellation:)
Presents a system dialog allowing the user to export a collection of Transferable items to files on disk.
Declaration
nonisolated func fileExporter<C, T>(isPresented: Binding<Bool>, items: C, contentTypes: [UTType] = [], onCompletion: @escaping (Result<[URL], any Error>) -> Void, onCancellation: @escaping () -> Void = { }) -> some View where C : Collection, T : Transferable, T == C.Element
Parameters
- isPresented:
A binding to whether the dialog should be shown.
- items:
Collection of values to be saved on disk.
- contentTypes:
The content types to use for the exported files. If empty, SwiftUI uses the content types from the
transferRepresentationproperty provided forTransferableconformance. - onCompletion:
A callback that will be invoked when the operation has succeeded or failed.
- onCancellation:
A callback that will be invoked if the user cancels the operation.
Discussion
In order for the dialog to appear, isPresented must be set to true. 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.
For example, a button that exports a collection of photos might look like this:
struct ExportPhotosButton: View {
@State private var isExporterPresented = false
var photos: [Photo]
var body: some View {
Button("Export Photos") {
isExporterPresented = true
}
.fileExporter(
isPresented: $isExporterPresented,
items: photos
) { result in
switch result {
case .success(let urls):
urls.forEach { print("Saved to \($0)") }
case .failure(let error):
print(error)
}
}
}
}
struct Photo: Transferable { ... }To further configure the dialog’s appearance and behavior, use these view modifiers: fileDialogDefaultDirectory(_:), fileDialogConfirmationLabel(_:), fileDialogMessage(_:), fileDialogBrowserOptions(_:), fileExporterFilenameLabel(_:), and fileDialogCustomizationID(_:).
See Also
Exporting to file
fileExporter(isPresented:document:contentType:defaultFilename:onCompletion:)fileExporter(isPresented:documents:contentType:onCompletion:)fileExporter(isPresented:document:contentType:defaultFilename:onCompletion:onCancellation:)fileExporter(isPresented:document:contentTypes:defaultFilename:onCompletion:onCancellation:)fileExporter(isPresented:documents:contentTypes:onCompletion:onCancellation:)fileExporter(isPresented:item:contentTypes:defaultFilename:onCompletion:onCancellation:)fileExporterFilenameLabel(_:)