Writing to "~/Library/Application Support" folder in Mac OS X 10.7
I can use NSFileManager to create a XYZ folder in "/Library/Application Support" in Mac OS X 10.5 and 10.开发者_JAVA技巧6. However, in 10.7, it says "You don't have permission to save XYZ in the folder 'Application Support'".
Does anyone know what I can do (e.g. which API to call) to get that permission to create the folder? Thanks.
Are you using sandboxing? If so, you’re writing to the wrong place. In either case, you should be using NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES)
to find the right path rather than hard-coding it.
As of OS X 10.6 or later, the recommended API to find the user's application support folder is:
NSError *error;
NSURL *appSupportDir = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&error];
NSLog(@"%@", appSupportDir);
// file://localhost/Users/abhi/Library/Application%20Support/
The path is not always ~/Library/Application Support
. Sometimes it's somewhere else (eg, when your app is running in a sandbox). Also, the Application Support directory might not exist yet. The above code will handle that and create it for you.
If the URL returned by that API is not writable, then you should log an exception to standard error, and allow your app to crash. This should only happen if the user has run out of disk space or something similar (in that case, it probably returns nil and an error object).
精彩评论