fileExporter
Presents a system dialog allowing the user to export a Transferable item to a file on disk.
Overloads
2 ways to call compose().fileExporter(). Each is one SwiftUI overload, picked by the arguments you pass.
compose().fileExporter<T>({
isPresented: Binding<boolean>,
item: T | null,
contentTypes?: string[],
defaultFilename?: string | null,
onCompletion: (arg0: SwiftResult<string>) => void,
onCancellation?: () => void,
})nonisolated func fileExporter<T>(
isPresented: Binding<Bool>,
item: T?,
contentTypes: [UTType] = [],
defaultFilename: String? = nil,
onCompletion: @escaping (Result<URL, any Error>) -> Void,
onCancellation: @escaping () -> Void = { }
) -> some View
where T : TransferableParameters
options.isPresented*Binding<boolean>A binding to whether the dialog should be shown.
options.item*T | nullThe item to be saved on disk.
options.contentTypesstring[]The optional content types to use for the exported file. If empty, SwiftUI uses the content types from the
transferRepresentationproperty provided forTransferableconformance.options.defaultFilenamestring | nullIf provided, the default name to use for the exported file, which the user will have an opportunity to edit prior to the export.
options.onCompletion*(arg0: SwiftResult<string>) => voidA callback that will be invoked when the operation has succeeded or failed.
options.onCancellation() => voidA 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 photo might look like this:
struct ExportPhotoButton: View {
@State private var isExporterPresented = false
var photo: Photo
var body: some View {
Button("Export Photo") {
isExporterPresented = true
}
.fileExporter(
isPresented: $isExporterPresented,
item: photo,
defaultFilename: "exported photo"
) { result in
switch result {
case .success(let url):
print("Saved to \(url)")
case .failure(let error):
print(error)
}
} onCancellation: {
print("Export cancelled")
}
}
}
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
More in Drag and drop.
- copyableSpecifies a list of items to copy in response to the system's Copy command.
- cuttableSpecifies an action that moves items to the Clipboard in response to the system's Cut command.
- documentLaunchSubtitleSets the subtitle displayed beneath the title on the document launch card, using a localized string resource.
- documentLaunchTitleSets the title displayed on the document launch card, using a localized string resource.
- draggableActivates this view as the source of a drag and drop operation.
- dropConfigurationConfigures a drop session.
- dropDestinationDefines the destination of a drag and drop operation that provides a drop operation proposal and handles the dropped content with a closure that you specify.
- fileExporterFilenameLabelOn macOS, configures the fileExporter with a label for the file name field.
- fileImporterPresents a system dialog for allowing the user to import multiple files.
- fileMoverPresents a system dialog for allowing the user to move an existing file to a new location.
- itemProviderProvides a closure that vends the drag representation to be used for a particular data element.
- pasteDestinationSpecifies an action that adds validated items to a view in response to the system's Paste command.