Accessing iCloud file on multiple devices via security-scoped bookmark

273 views Asked by At

I have a security-scoped bookmark, stored in my app's iCloud directory, that points to a file also stored in iCloud, so the bookmark and the file are available even after reinstalling the app. Now I would like to access the bookmark and the file on another device, synced using the same iCloud account.

Is it possible to access such file via bookmark created on another device? Apple's documentation is hard to grasp in that topic. Documentation says:

For an app-scoped bookmark, no sandboxed app other than the one that created the bookmark can obtain access to the file-system resource that the URL (obtained from the bookmark) points to. Specifically, a bookmark created with security scope fails to resolve if the caller does not have the same code signing identity as the caller that created the bookmark.

For a document-scoped bookmark, any sandboxed app that has access to the bookmark data itself, and has access to the document that owns the bookmark, can obtain access to the resource.

So I would like to create document-scoped bookmark, but all my attempts so far end with errors. Is document-scoped bookmark reserved to macOS only and not available on iOS?

UPDATE

I'm saving the bookmark using the code:

let bookmarkData = try url.bookmarkData(options: .minimalBookmark, includingResourceValuesForKeys: nil, relativeTo: nil)
try bookmarkData.write(to: bookmarkUrl)

Where url is returned by UIDocumentBrowserViewController (and points to iCloud file that is already downloaded and available) and bookmarkUrl points to iCloud app directory bookmark location. Then I read it using the code:

let bookmarkData = try Data(contentsOf: bookmarkUrl)
var isStale = false
let url = try URL(resolvingBookmarkData: bookmarkData, bookmarkDataIsStale: &isStale)

It works fine between reinstalling the app on the same device running iOS 13.6. On the other device that is running iOS 14.4, even after reinstalling the app I'm getting an error on the last line:

Error Domain=NSCocoaErrorDomain Code=257 "The file couldn’t be opened because you don’t have permission to view it."

Looks like I'm missing something related to new iOS 14 permission rules. Also, bookmark created on one phone can be accessed on the other one but trying to resolve resource url from the bookmark ends with the error:

Error Domain=NSCocoaErrorDomain Code=4 "The file doesn’t exist."
0

There are 0 answers