FilePicker is a SwiftUI view modifier that allows you to open a file picker and open or save a file from the user's device.
- Swift 5.9+ (Xcode 15+)
- iOS 15+, macOS 12+
dependencies: [
.package(url: "https://github.com/0xWDG/FilePicker.git", branch: "main"),
],
targets: [
.target(name: "MyTarget", dependencies: [
.product(name: "FilePicker", package: "FilePicker"),
]),
]
- In Xcode, open your project and navigate to File → Swift Packages → Add Package Dependency...
- Paste the repository URL (
https://github.com/0xWDG/FilePicker
) and click Next. - Click Finish.
import SwiftUI
import FilePicker
struct ContentView: View {
// MARK: Filepicker
@State var filePickerOpen = false
@State var filePickerFiles: [URL] = []
var body: some View {
VStack {
Text("Open a file :)")
.padding()
Button("Open", systemImage: "square.and.arrow.down") {
filePickerOpen.toggle()
}
ForEach(filePickerFiles, id: \.self) { file in
Text(file.lastPathComponent)
}
}
.padding()
.filePicker(
isPresented: $filePickerOpen,
files: $filePickerFiles,
types: [.json, .text], // Optional (default: .json)
allowsMultipleSelection: false // Optional (default: false)
)
.onChange(of: $filePickerFiles.wrappedValue) { newValue in
print(newValue)
}
}
}
import SwiftUI
import FilePicker
struct ContentView: View {
// MARK: Filepicker
@State var filePickerOpen = false
var filePickerFileName = "test.txt"
var filePickerFileData = var filePickerData = Data("Hello, World!".utf8)
var body: some View {
VStack {
Text("Save a file :)")
.padding()
Button("Save", systemImage: "square.and.arrow.up") {
filePickerOpen.toggle()
}
}
.padding()
.filePicker(
isPresented: $filePickerOpen,
fileName: filePickerFileName,
data: filePickerData,
types: [.text]
)
}
}
🦋 @0xWDG 🐘 mastodon.social/@0xWDG 🐦 @0xWDG 🧵 @0xWDG 🌐 wesleydegroot.nl 🤖 Discord
Interested learning more about Swift? Check out my blog.