Skip to content

Instantly share code, notes, and snippets.

@scriptpapi
Created April 26, 2021 21:25
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save scriptpapi/d4d2647aedd761831eeaf1450c299887 to your computer and use it in GitHub Desktop.
Save scriptpapi/d4d2647aedd761831eeaf1450c299887 to your computer and use it in GitHub Desktop.
Document Picker for SwiftUI
import Foundation
import SwiftUI
import UIKit
struct DocumentPicker: UIViewControllerRepresentable {
@Binding var filePath: URL?
func makeCoordinator() -> DocumentPicker.Coordinator {
return DocumentPicker.Coordinator(parent1: self)
}
func makeUIViewController(context: UIViewControllerRepresentableContext<DocumentPicker>) -> UIDocumentPickerViewController {
let picker = UIDocumentPickerViewController(documentTypes: ["public.item"], in: .open)
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