Skip to content

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.

License

Notifications You must be signed in to change notification settings

0xWDG/FilePicker

Repository files navigation

FilePicker

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 Package Manager License

Requirements

  • Swift 5.9+ (Xcode 15+)
  • iOS 15+, macOS 12+

Installation (Pakage.swift)

dependencies: [
    .package(url: "https://github.com/0xWDG/FilePicker.git", branch: "main"),
],
targets: [
    .target(name: "MyTarget", dependencies: [
        .product(name: "FilePicker", package: "FilePicker"),
    ]),
]

Installation (Xcode)

  1. In Xcode, open your project and navigate to FileSwift PackagesAdd Package Dependency...
  2. Paste the repository URL (https://github.com/0xWDG/FilePicker) and click Next.
  3. Click Finish.

Usage (Open file)

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)
        }
    }
}

Usage (Save file)

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]
        )
    }
}

Contact

🦋 @0xWDG 🐘 mastodon.social/@0xWDG 🐦 @0xWDG 🧵 @0xWDG 🌐 wesleydegroot.nl 🤖 Discord

Interested learning more about Swift? Check out my blog.

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Languages