UIDocumentPickerViewController not working on Mac Catalyst?

I have used UIDocumentPickerViewController in application in order to retrieve files from iCloud Drive. Since i'm using SwiftUI only the picker is incapsulated in the within SwiftUI's View with a protocol UIViewControllerRepresentable.


It works perfectly on iOS/iPadOS, but when i'm trying to launch application on Mac via Catalyst i'm just seeing an empty view on picker's modal view. So just modal pop up with no content at all.


Have anyone faced same issue with this picker?

import SwiftUI
struct 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 SwiftUI
final 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

Hi Cesare, thank for your help. Where is
Code Block
DocPickerViewController
class declarated? You use in
Code Block
.sheet
statement, but I don't know where to declare this class.
UIDocumentPickerViewController not working on Mac Catalyst?
 
 
Q