Post

Replies

Boosts

Views

Activity

Reply to UIDocumentPickerViewController not working on Mac Catalyst?
import SwiftUIstruct ContentView: View { @State private var isFilePickerShown = false @State private var picker = DocumentPicker() var body: some View { VStack { Button(action: { self.isFilePickerShown.toggle() #if targetEnvironment(macCatalyst) UIApplication.shared.windows[0].rootViewController!.present(self.picker.viewController, animated: true) #endif }) { Image(systemName: "rectangle.and.paperclip").resizable().frame(width: 70, height: 70) } } .sheet(isPresented: $isFilePickerShown, onDismiss: {self.isFilePickerShown = false}) { DocPickerViewController(callback: self.filePicked, onDismiss: { self.isFilePickerShown = false }) } } func filePicked(_ url: URL) { print("\nThe url is: \(url)") }}and now create new swiftui file DocumentPicker for MacOS: import SwiftUIfinal class DocumentPicker: NSObject, UIViewControllerRepresentable { typealias UIViewControllerType = UIDocumentPickerViewController lazy var viewController:UIDocumentPickerViewController = { // For picked only folder let vc = UIDocumentPickerViewController(documentTypes: ["public.folder"], in: .open) // For picked every document// let vc = UIDocumentPickerViewController(documentTypes: ["public.data"], in: .open) // For picked only images// let vc = UIDocumentPickerViewController(documentTypes: ["public.image"], in: .open) vc.allowsMultipleSelection = false// vc.accessibilityElements = [kFolderActionCode]// vc.shouldShowFileExtensions = true vc.delegate = self return vc }() func makeUIViewController(context: UIViewControllerRepresentableContext<DocumentPicker>) -> UIDocumentPickerViewController { viewController.delegate = self return viewController } func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: UIViewControllerRepresentableContext<DocumentPicker>) { }}extension DocumentPicker: UIDocumentPickerDelegate { func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { print(urls) } func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) { controller.dismiss(animated: true) { } print("cancelled") }}This code popup a document picker in both iOS and MacOS using catalyst. With MacOS Catalyst work with DocumentPicker Class. Cesare Piersigilli
Feb ’20