I am working on an app for OS X 10.9 with swift, sandboxed.
The app needs access to a SQLite database file. I let the user choose/open a file with NSOpenPanel. I then save the file path with NSUserDefaults for later use.
I want this file to be opened automatically every time when the app is started again. I get the stored path from the NSUserDefault, but when I open the file with this path I get an error, saying I have no permission to access the file.
(it is working without sandboxing)
It looks like bookmark's are the solution to my problem.
Is there a good tutorial how to use bookmark's with swift for an osx app? Any other suggestion?
Here is my answer that I've just got working in Swift 3 with a little help from http://swiftrien.blogspot.com/2015/07/persisting-file-access-rights-between.html
To use this code you must first call NSOpenPanel so the user can select which folders to give you access to. The NSOpenPanel must be stored as a bookmark and saved to disk.
When you restart the application you must call
then your app will have the same level of access as it did when the user selected the folder. Hope this helps someone.
security scoped bookmarks is exactly the way to go. good starting point is apple documentation on AppStore sandbox (which includes sample code) and class reference of NSFileManager.
you then will not store the path in user defaults but the binary data of the bookmark.