Search code examples
macoscocoasandbox

macOS store sandbox app uses NSOpenPanel to select download file folder, but can not access the folder again


My app is to download file from a web site.

I enabled the sandbox of the project for macOS store.

enter image description here

The app will trigger the NSOpenPanel to ask user select the folder where the download files(all file list store in a sqlite file) are saved to. for example:

/home/mymac/myfolder

Everything is OK. If I close the app and reopen it, I hope it can be continue to download the files (in the sqlite file).

but it reports error: setting security information: Operation not permitted

it looks like the system does not allow the app access the folder

/home/mymac/myfolder

again.

If I use NSOpenPanel to select the system download folder

/home/mymac/Downloads

close the app and reopen the app, everything works fine. It looks like the system only allow the app access the folder

/home/mymac/Downloads

again.

Your comment welcome


Solution

  • You need to obtain a bookmark to the URL and store it persistently. When your application opens, retrieve the URL from the stored bookmark.

    The way to do it is described in docs: Locating Files Using Bookmarks

    You need only 2 methods:

    - (NSData*)bookmarkForURL:(NSURL*)url
    - (NSURL*)urlForBookmark:(NSData*)bookmark
    

    You can store the bookmark in a .plist file or even in UserDefaults if you don't expect to have lots of bookmarks.