Created
April 26, 2021 21:25
-
-
Save scriptpapi/d4d2647aedd761831eeaf1450c299887 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 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