Failure to create bookmarkData in sandboxed app

  • NSError *error = nil;
        NSURLBookmarkCreationOptions options = NSURLBookmarkCreationSuitableForBookmarkFile | NSURLBookmarkCreationWithSecurityScope;
        NSData *bookmarkData = [self.url bookmarkDataWithOptions:options
                              includingResourceValuesForKeys:nil
                                              relativeToURL:nil
                                                      error:&error];

    This code works fine in a non-sandboxed app (without the NSURLBookmarkCreationWithSecurityScope option). It also works fine in a sandboxed app, when I leave out the NSURLBookmarkCreationWithSecurityScope. However when I execute the code as written above in a sandboxed app the bookmarkDataWithOptions:etc… method returns nil, and an error: Domain=NSCocoaErrorDomain Code=256 "The file “xxx” couldn’t be opened."

    self.url is a copy of a fileURL requested from an NSDocument.

    I've added the com.apple.security.files.bookmarks.app-scope entitlement to the entitlements file, and checked "Use entitlements file". I want to create app scope bookmarks and I think the code above is correct. Is there anything else that needs to be set up, or am I misunderstanding anything? I can't find anything in the docs or by searching for similar problems (they turn up, but nowhere do I see a replica if this precise case,  nor any pointers in somewhat similar cases that led me to a solution).

    -António
  • On 5/11/13 8:56 AM, Antonio Nunes wrote:
    >
    > NSError *error = nil; NSURLBookmarkCreationOptions options > NSURLBookmarkCreationSuitableForBookmarkFile |
    > NSURLBookmarkCreationWithSecurityScope; NSData *bookmarkData = [self.url
    > bookmarkDataWithOptions:options includingResourceValuesForKeys:nil
    > relativeToURL:nil error:&error];

    Have you tried using just NSURLBookmarkCreationWithSecurityScope as options?

    Regards
    Markus
    --
    __________________________________________
    Markus Spoettl
  • On 11 May, 2013, at 09:50 , Markus Spoettl <ms_lists...> wrote:

    >> NSError *error = nil; NSURLBookmarkCreationOptions options =
    >> NSURLBookmarkCreationSuitableForBookmarkFile |
    >> NSURLBookmarkCreationWithSecurityScope; NSData *bookmarkData = [self.url
    >> bookmarkDataWithOptions:options includingResourceValuesForKeys:nil
    >> relativeToURL:nil error:&error];
    >
    > Have you tried using just NSURLBookmarkCreationWithSecurityScope as options?

    When I do that, the bookmarkData gets created. I then found that after loading the data back in when running the app again, then if I wanted to use the URL, [userBookmark.url startAccessingSecurityScopedResource] returned NO. However, it turns out that I was not reading the data back in properly. I had written this:

        self.url = [NSURL URLByResolvingBookmarkData:urlData
                                                      options:NSURLBookmarkResolutionWithoutUI
                                                relativeToURL:nil
                                          bookmarkDataIsStale:&bookmarkIsStale
                                                        error:&error];

    Turns out had overlooked that another option needs to be passed in:

        self.url = [NSURL URLByResolvingBookmarkData:urlData
                                                      options:NSURLBookmarkResolutionWithoutUI | NSURLBookmarkResolutionWithSecurityScope
                                                relativeToURL:nil
                                          bookmarkDataIsStale:&bookmarkIsStale
                                                        error:&error];

    Duh.

    So, it looks like it is working. Except: now, if the file is moved from where it was when the bookmark data was created, it is no longer found. When passing in NSURLBookmarkCreationSuitableForBookmarkFile as an option in the non sandboxed version this does retain access to the file when the file is moved. Neither do the documents suggest that NSURLBookmarkCreationSuitableForBookmarkFile and NSURLBookmarkCreationWithSecurityScope are mutually exclusive options. The question then remains, are they mutually exclusive? —Thus making bookmarks in sandboxed apps considerably less flexible than in non-sandboxed apps.) Or should this be possible but something else is preventing this from working?

    - António
previous month may 2013 next month
MTWTFSS
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
Go to today