问题
My app reads iTunes' XML Library file under
/Users/username/Music/iTunes/iTunes Music Library.xml
However, if the iTunes Library is stored on a different hard disk, so will this file. The sandbox doesn't allow me to access this file, unless the user grants it.
This is done using a NSOpenPanel
.
However, now I'd have to ask the user for permission to access this file each time he opens up the app, which is pretty annoying.
Is there a way to permanently grant access to a specific file?
EDIT
For example, if you open a file in another app, if you quit it the app will open the same file again. Something like this would be useful.
回答1:
What you need is to use security scoped bookmarks. The outline of what you need to do is:
- Check if you have a saved bookmark (see below), if so activate it. Done.
- Have the user select a file/folder using
NSOpenPanel
- Take the URL that is returned and save it to disk as a security scoped bookmark - you save it somewhere in your container, your
NSUserDefaults
is one choice.
That's it. You need to read Apple's documentation to understand the details.
来源:https://stackoverflow.com/questions/16842316/access-to-the-same-file-after-restart-in-the-sandbox