Open folder in UIKit for Mac

Does anyone have an idea how to let the user open a folder in an app built with UIKit for Mac?

My use case is that I want the user to be able to specify a directory on the Mac, from where the app will import data into the app.


I don't seem to be able to use NSOpenPanel when building for UIKit for Mac. Is there a workaround??


Any help much appreciated.

Answered by J0hn in 612382022
Mac Users are used to Drag and Drop as a primary means of interaction, and as you've discovered the act of dropping the folder onto your app will give you app sandbox permission to read the folder and its contents.

If you want to provide an open dialog use UIDocumentPickerViewController. This picker will display like NSOpenPanel from AppKit, but is compatible with iPad and iPhone builds of your app too.

Ok, there is a work-around: Make the app a drop-target, and then drop the folder onto the view in your app you designated as drop-target. For this to work, you need to give focus to the app FIRST, then drag the folder onto the app. It will NOT work if the app has no focus, and you drag the folder onto the app, at least not in the current beta.

Accepted Answer
Mac Users are used to Drag and Drop as a primary means of interaction, and as you've discovered the act of dropping the folder onto your app will give you app sandbox permission to read the folder and its contents.

If you want to provide an open dialog use UIDocumentPickerViewController. This picker will display like NSOpenPanel from AppKit, but is compatible with iPad and iPhone builds of your app too.
Yes, I found now Apple documentation for that use case. You can do that via

Code Block
let folderPicker = UIDocumentPickerViewController(documentTypes: [kUTTypeFolder as String], in: .open)

Open folder in UIKit for Mac
 
 
Q