Skip to content

Instantly share code, notes, and snippets.

@BomberFish
Forked from scriptpapi/DocumentPicker.swift
Last active May 8, 2025 14:34
Show Gist options
  • Save BomberFish/d500c1e41410b9df4acb27943fe02641 to your computer and use it in GitHub Desktop.
Save BomberFish/d500c1e41410b9df4acb27943fe02641 to your computer and use it in GitHub Desktop.
Document Picker for SwiftUI
import SwiftUI
import UniformTypeIdentifiers
struct DocumentPicker: UIViewControllerRepresentable {
@Binding var filePath: URL?
var utTypes: [UTType]
func makeCoordinator() -> DocumentPicker.Coordinator {
return DocumentPicker.Coordinator(parent1: self)
}
func makeUIViewController(context: UIViewControllerRepresentableContext<DocumentPicker>) -> UIDocumentPickerViewController {
let picker = UIDocumentPickerViewController(forOpeningContentTypes: utTypes)
picker.allowsMultipleSelection = false
picker.delegate = context.coordinator
return picker
}
func updateUIViewController(_ uiViewController: DocumentPicker.UIViewControllerType, context: UIViewControllerRepresentableContext<DocumentPicker>) {
}
class Coordinator: NSObject, UIDocumentPickerDelegate {
var parent: DocumentPicker
init(parent1: DocumentPicker){
parent = parent1
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
parent.filePath = urls[0]
print(urls[0].absoluteString)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment