-
-
Save BomberFish/d500c1e41410b9df4acb27943fe02641 to your computer and use it in GitHub Desktop.
Document Picker for SwiftUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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