How it works
FileDialogBrowserOptions is an option set that tunes the chrome and behavior of the system file browser that SwiftUI presents for import and export operations. When you open a panel with a modifier like fileImporter or fileExporter, the dialog ships with platform defaults; FileDialogBrowserOptions lets you override those defaults to control things like whether hidden files are shown, whether the user may pick directories, and how selection is enforced. Reach for it when the standard dialog is close to what you want but you need to relax or tighten one of its built-in rules.
Apply the options with fileDialogBrowserOptions(_:)
The options are injected into the environment with the fileDialogBrowserOptions(_:) view modifier, which applies to any file dialog presented from that part of the hierarchy. In the example it is attached after the presentation modifier as
.fileDialogBrowserOptions(.includeHiddenFiles), so the next dialog raised within that view adopts the configuration.Pair it with the dialog presenter
FileDialogBrowserOptions only takes effect when there is a dialog to configure, so it sits alongside a presenter modifier. Here the panel is raised by
.fileImporter(isPresented: $importing, allowedContentTypes: [.folder]), and the browser options modifier on the same chain customizes that importer's browser before it appears.Combine flags as an option set
Because FileDialogBrowserOptions conforms to OptionSet, you select behaviors as static members and union them together. The example passes the single member
.includeHiddenFiles; you can OR several members in a single value to layer multiple behaviors, such as also permitting directory selection or disabling multiple selection.Let the included files reach your result handler
The options shape what the browser exposes, which in turn changes what can flow back through the dialog's completion. In the example the selection arrives in the
resultclosure of fileImporter, whereif case .success(let url) = resultunwraps the chosen URL andpicked = url.lastPathComponentrecords it — including items the option set made selectable.
.fileDialogBrowserOptions(.includeHiddenFiles) line and reopen the dialog with the "Choose Folder" button: dot-prefixed hidden folders disappear from the browser, showing exactly what the option toggled on.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct FileDialogBrowserOptionsDemo: View {
@State private var importing = false
@State private var picked = "No folder selected"
var body: some View {
VStack(spacing: 16) {
Image(systemName: "folder.badge.gearshape")
.font(.largeTitle)
Text(picked)
.font(.callout)
Button("Choose Folder") { importing = true }
.buttonStyle(.borderedProminent)
}
.padding()
.fileImporter(isPresented: $importing, allowedContentTypes: [.folder]) { result in
if case .success(let url) = result { picked = url.lastPathComponent }
}
.fileDialogBrowserOptions(.includeHiddenFiles)
}
}