4

I'm working with document based security scoped bookmark to store references to files in a document. I create them like so:

_bookmark = [_url bookmarkDataWithOptions: NSURLBookmarkCreationWithSecurityScope 
           includingResourceValuesForKeys: nil
                            relativeToURL: relativeURL
                                    error: error];

The relativeURL contains the URL to the document that will store the bookmark.

I later resolve the bookmark using:

resolvedURL = [NSURL URLByResolvingBookmarkData: bookmark 
                                        options: NSURLBookmarkResolutionWithoutMounting | NSURLBookmarkResolutionWithSecurityScope
                                  relativeToURL: url 
                            bookmarkDataIsStale: &isStale
                                          error: error];

This works as long as the document and referred files stay on the initial machine. I can then move and rename them and they keep working.

On the other hand, if I zip up the folder containing the document and files, and extract them on another machine, the bookmarks don't resolve.

The presented error is code 4: File doesn't exist. I verified that the document still contains its security scope attribute and it does. Of course both the document and referred file actually exist.

I tried creating the bookmark data from relative URLs, but that doesn't change anything. Requesting the URL from the bookmark always shows an absolute URL.

How can I make sure that the bookmarks resolve when the document and files are transferred to another machine?

Update

This problem has been confirmed by Apple. It's tracked under radar: 24126953. Unfortunately, I was told they internally do not agree on whether this should get fixed or not. I hope they will someday.

Remco Poelstra
  • 859
  • 6
  • 20
  • That sounds correct to me. You'll have to re-create the bookmarks after the new files are added. – trojanfoe Dec 16 '15 at 13:23
  • I don't really understand you. I did not add new files. I create bookmarks for some files and store the document. I then zip up the directory containing both the document and the files and transfer the zip to another machine. After unpacking and opening the document, the bookmarks can not be resolved. Probably because it tries to resolve them in an absolute fashion, instead of relative to the document (although the document's URL needs to be provided). – Remco Poelstra Dec 16 '15 at 19:12
  • If they were resolved don't you think that would counteract the security they are supposed to enforce? The sandboxing mechanism is used to restrict an app's access to files and if it were possible to add files that gave that access, outside of the normal user file-dialog mechanism, then that feature would be broken. While I don't know the precise details of the security-scoped bookmarks (even though I have implemented an app that uses them), I would expect the bookmark to be tied to the machine. – trojanfoe Dec 16 '15 at 19:26
  • 4
    The documentation from Apple states: _A document-scoped bookmark can be resolved by any app that has access to the bookmark data itself and to the document that owns the bookmark. This supports portability, allowing a user, for example, to send a document to another user; the document’s secure bookmarks remain usable for the recipient._ I think it's very important that that actually works, or how are users supposed to share data? – Remco Poelstra Dec 16 '15 at 19:52
  • 1
    This is from [here](https://developer.apple.com/library/mac/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html) BTW – Remco Poelstra Dec 16 '15 at 19:52
  • I stand corrected; I did not know about that feature. – trojanfoe Dec 16 '15 at 20:13
  • Did you ever find a solution for this? – mralexhay Apr 04 '20 at 08:52
  • Apple confirmed this bug (via DTS), but are reluctant to solve it. – Remco Poelstra Apr 04 '20 at 11:06
  • The bug ID is FB6006349 – Remco Poelstra Apr 04 '20 at 11:13
  • Did you call startAccessingSecurityScopedResource before URLByResolvingBookmarkData? Like the doc says here: https://developer.apple.com/documentation/foundation/nsurl/1572035-urlbyresolvingbookmarkdata – malhal Jun 29 '21 at 10:09
  • 1
    I think the docs say you have to call `startAccessingSecurityScopedResource` /after/ `URLByResolvingBookmarkData`. Otherwise there is no URL to call that method on, but in this case no URL is returned as the bookmark can't be resolved. – Remco Poelstra Jun 30 '21 at 12:24

0 Answers0