Search code examples
objective-cappstore-sandboxsecurity-scoped-bookmarks

Security scoped bookmark stopped working on Yosemite 10.10


I am using the following code to generate security scoped bookmarks. This worked fine on 10.8 and 10.9 but has stopped working on 10.10. I am at a loss as to what to check?

 NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setAllowsMultipleSelection:NO];
[panel setCanChooseDirectories:YES];
[panel setCanChooseFiles:NO];
[panel setResolvesAliases:YES];
[panel setCanCreateDirectories:YES];
[panel setTitle:@"Choose a directory as your input folder"];
[panel setPrompt:@"Choose"];

NSInteger result = [panel runModal];

if (result == NSFileHandlingPanelOKButton){
    NSURL *urlPath = [[panel URLs] objectAtIndex:0];
    NSError *error = nil;
    NSData *bookmark = nil;
    bookmark = [urlPath bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
                         includingResourceValuesForKeys:nil
                                          relativeToURL:nil 
                                                  error:&error];

    if (error) {
        [NSApp presentError:error];
    }

    BOOL bookmarkDataIsStale;
    NSURL *url = [NSURL URLByResolvingBookmarkData:     bookmark
                                           options:NSURLBookmarkResolutionWithSecurityScope
                                     relativeToURL:nil
                               bookmarkDataIsStale:&bookmarkDataIsStale                                                              error:nil];
}

I have enabled the App Sandbox entitlement and added the com.apple.security.files.user-selected.read-write and com.apple.security.files.bookmarks.app-scope entitlements.

The above is generating a URL, but it is not security scoped. So when logged it is the usual file path, not as was previously occurring with the ? and then heaps of characters afterwards.

Any help or ideas are appreciated


Solution

  • I have resolved this. The above code was used to store the security scoped bookmark as NSURL using [[NSUserDefaults standardUserDefaults] setURL:url forKey:@"basePath"]. This worked fine in 10.8 and 10.9 but no longer works in 10.10. If I save the bookmark data as a data object and then retrieve it as required and convert to an NSURL then all works fine again.