Overloads
3 ways to call compose().fileImporter(). Each is one SwiftUI overload, picked by the arguments you pass.
compose().fileImporter({
isPresented: Binding<boolean>,
allowedContentTypes: string[],
allowsMultipleSelection: boolean,
onCompletion: (result: SwiftResult<string[]>) => void,
})nonisolated func fileImporter(
isPresented: Binding<Bool>,
allowedContentTypes: [UTType],
allowsMultipleSelection: Bool,
onCompletion: @escaping (Result<[URL], any Error>) -> Void
) -> some ViewParameters
options.isPresented*Binding<boolean>A binding to whether the dialog should be shown.
options.allowedContentTypes*string[]The list of supported content types which can be imported.
options.allowsMultipleSelection*booleanWhether the importer allows the user to select more than one file to import.
options.onCompletion*(result: SwiftResult<string[]>) => voidA callback that will be invoked when the operation has succeeded or failed. To access the received URLs, call
startAccessingSecurityScopedResource. When the access is no longer required, callstopAccessingSecurityScopedResource. - term result: AResultindicating whether the operation succeeded or failed.
Discussion
In order for the dialog to appear, isPresented must be 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 onCompletion will not be
called.
For example, a button that allows the user to choose multiple PDF files for the application to combine them later, might look like this:
struct PickPDFsButton: View {
@State private var showFileImporter = false
var handlePickedPDF: (URL) -> Void
var body: some View {
Button {
showFileImporter = true
} label: {
Label("Choose PDFs to combine", systemImage: "doc.circle")
}
.fileImporter(
isPresented: $showFileImporter,
allowedContentTypes: [.pdf],
allowsMultipleSelection: true
) { result in
switch result {
case .success(let files):
files.forEach { file in
// gain access to the directory
let gotAccess = file.startAccessingSecurityScopedResource()
if !gotAccess { return }
// access the directory URL
// (read templates in the directory, make a bookmark, etc.)
handlePickedPDF(file)
// release access
file.stopAccessingSecurityScopedResource()
}
case .failure(let error):
// handle error
print(error)
}
}
}
}To further configure the dialog's appearance and behavior, use these
view modifiers: fileDialogDefaultDirectory(_:),
fileDialogConfirmationLabel(_:), fileDialogMessage(_:),
fileDialogBrowserOptions(_:), fileDialogURLEnabled(_:),
fileDialogImportsUnresolvedAliases(_:), 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.
- fileExporterPresents a system dialog allowing the user to export a Transferable item to a file on disk.
- fileExporterFilenameLabelOn macOS, configures the fileExporter with a label for the file name field.
- 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.