How to show open and save dialog?

I tried to use following code:


struct FilePickerView: UIViewControllerRepresentable {

    func makeUIViewController(context: Context) -> UIDocumentPickerViewController {
        let documentPicker = UIDocumentPickerViewController(documentTypes: [(kUTTypeImage as String)], in: .open)
        return documentPicker
    }

    func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: Context) {
        // TODO
    }
}


And present it like this:


.sheet(isPresented: $showFilePicker) {
    FilePickerView()
}


But on macOS I see blank screen centered in my app window. I solution working on Catalyst.

Are you using Catalyst to make a Mac version of an iOS SwiftUI app or are you making a regular Mac app with SwiftUI? I don't have an answer for you if you're using Catalyst.


If you're making a regular Mac app, your FilePickerView won't work on Mac because you're using a UIKit view and the UIKit document picker. An AppKit view should use NSViewRepresentable and should use NSOpenPanel and NSSavePanel for opening and saving files. The answer by Matteo Paccini to the following Stack Overflow question should help you, and the other answer could help if you're using Catalyst:


stackoverflow.com/questions/56645819/how-to-open-file-dialog-with-swiftui-on-platform-uikit-for-mac

Yes, I need Catalyst version. That is why NSPanel will not work.


As for Stack Overflow answers I don't want to use hacks until I get official answer that this task can not be solved without hacks.

Hello Ivan85,

I am also trying to implement the open file dialog using Catalyst and experience the same problem.

Have you found a solution in the meantime? It's hard since, I can neither find any good example nor do my own tries succeed.
Your are actually my best bet, hope you had more luck with this challenge.

Did you guys find any solution for this?
How to show open and save dialog?
 
 
Q