CodeDezk CodeDezk - 1 year ago 200
Objective-C Question

Security-Scoped Bookmarks for a directory

I need to give full read/write permission for a directory where the application write some file to this directory. I read that using sandboxed application it required to Enable Security-Scoped Bookmark and URL Access to access a folder after relaunch the app.

So I am trying to implement it based on the code here with some minor modification What is the correct way to handle stale NSURL bookmarks?

NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseDirectories:YES];
[openDlg setCanCreateDirectories:YES];
[openDlg setAllowsMultipleSelection:FALSE];
if ( [openDlg runModal] == NSOKButton )
NSArray *files = [openDlg URLs];

NSString* dirPath =[[files objectAtIndex:0] path];// absoluteString];
BOOL isDirectory;
NSFileManager* manager = [NSFileManager defaultManager];

NSString *Dir = [dirPath stringByAppendingPathComponent:@"ScreenCaptures"];
if (![manager fileExistsAtPath:Dir isDirectory:&isDirectory] || !isDirectory)
NSError *error = nil;

[manager createDirectoryAtPath:Dir
if (error)
NSLog(@"Error creating directory snap path: %@", [error localizedDescription]);


NSURL *url = [NSURL URLWithString:[Dir stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSData *bookmark = nil;
NSError *error = nil;
bookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
relativeToURL:nil // Make it app-scoped
if (error) {
NSLog(@"Error creating bookmark for URL (%@): %@", url, error);
[NSApp presentError:error];

NSLog(@"bookmark: %@", bookmark);
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:bookmark forKey:@"bookmark"];


But the above code giving me the error

016-08-20 02:19:53.390 FileAccess[635:85753] modalSession has been exited prematurely - check for a reentrant call to endModalSession:
2016-08-20 02:19:59.979 FileAccess[635:85753] Error creating bookmark for URL (/Users/development/Documents/c/ScreenCaptures): Error Domain=NSCocoaErrorDomain Code=262 "Scoped bookmarks can only be made with file URLs" UserInfo={NSURL=/Users/development/Documents/c/ScreenCaptures, NSDebugDescription=Scoped bookmarks can only be made with file URLs}
2016-08-20 02:20:00.021 FileAccess[635:85753] CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme
2016-08-20 02:20:04.967 FileAccess[635:85753] bookmark: (null)

What could be the problem?, anything wrong on above code.

Answer Source

Your second error message tells you what is wrong - you haven't used a file:// URL.

This can be fixed by creating the URL properly from your path variable, however you will probably be better of sticking with URLs throughout and not doing the URL -> path -> URL transformation. All the operations you've used the path for can be done directly with URLs, just check the documentation for NSFileManager and NSURL. The only one which may be non-obvious is using NSURL's checkResourceIsReachableAndReturnError: rather than NSFileManager's fileExistsAtPath:, however read the documentation for checkResourceIsReachableAndReturnError: carefully and take its advice.

Making these changes should address at least three of the errors you have reported.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download