2
votes

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

1
I've seen the behavior you described in my own app, but the URLs do still appear to have security scope (they can grant access to the file). You just can't see the security token in the query string anymore. I found this thread on the Apple Dev Forums about it: devforums.apple.com/message/1059959Dov
This still isn't resolved, but seems to be the same issue as you have linked to. Thank you so much.Anthony W
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.Anthony W
You should post this as an answer, rather than a comment, and accept it. Also, do you see the "applesecurityscope" argument in the query string now? Because I've always been storing the bookmarks as data, and they disappeared for me in Yosemite.Dov
Thanks Dov, sorry I am new to this. I don't see the applesecurityscopeargument any longer either in 10.10 but the bookmarks are working fine.Anthony W

1 Answers

2
votes

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.