Reputation: 14954
My OSX application is intended to monitor a folder on the computer which the user has selected. Very simply, I have a function which scans the directory:
- (NSError*)scan:(NSString*)dir {
NSError *err = nil;
NSArray *filenames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dir error:&err];
for(NSString* fn in filenames) {
NSString *fp = [dir stringByAppendingPathComponent:fn];
BOOL isDir;
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:fp isDirectory:&isDir];
if(exists && isDir) {
[self scan:fp];
}
else {
[self handleFile:fp];
}
}
NSLog(@"Scanning %@: %@",dir,err);
return err;
}
When I first start the application, I present a UI to select the folder and then immediately scan it. The operation works great (no errors).
When I restart the application, I attempt to scan the directory again, but this time the NSLog
spits out an error:
Scanning /Users/zane/Dropbox/Gifs: Error Domain=NSCocoaErrorDomain Code=257 "The file “Gifs” couldn’t be opened because you don’t have permission to view it." UserInfo=0x608000476d40 {NSFilePath=/Users/zane/Dropbox/Gifs, NSUserStringVariant=(
Folder
), NSUnderlyingError=0x60800005d3d0 "The operation couldn’t be completed. (OSStatus error -5000.)"}
Note that the path has not changed.
I suspect that the problem has to do with .entitlements
. Here's what I've got:
Upvotes: 1
Views: 639
Reputation: 6707
If you are going to use the same file or folder the user already selected, then you need to save the path as a security-scoped bookmark when the user selects that path. Read its bookmark data when the user restarts the application in order to use the saved path. There's a description of security-scoped bookmark at the middle of this page. Use the URLByResolvingBookmarkData method of NSURL to return a security-scoped bookmark. Use the bookmarkDataWithOptions method or equivalent of NSURL to resume the security-scoped bookmark.
Upvotes: 3