Saving the iCloud for iOS Security Tied URL (UIDocumentPickerViewController)

I am trying to store the security bound url returned by iCloud document picker (UIDocumentPickerViewController)

The documentation states:

If the url is not the ubiquitous url, save the bookmark to a file using bookmarkDataWithOptions: includingResourceValuesForKeys: relativeToURL: error: method and pass to NSURLBookmarkCreationWithSecurityScope option. Calling this method creates a bookmark containing a scoped URL that you can use to open the file without further user intervention.

However, the compiler says NSURLBookmarkCreationWithSecurityScope is not supported on iOS.

Does anyone know what's going on here ...?

+3


source to share


2 answers


After further digging, an option is obtained: it is
NSURLBookmarkCreationWithSecurityScope


NOT required at all when creating bookmark data in IOS. This is an OS X variant. You can simply pass nil for the option field. I think Apple's doc is confusing at best.
However, you need to call:
startAccessingSecurityScopedResource


before creating the bookmark and make sure the call returns 1 (success) before continuing. Otherwise, bookmark creation will fail. Here's some sample code:
if([url startAccessingSecurityScopedResource]==1){
NSError *error;
NSData *bookmark = [url bookmarkDataWithOptions:nil
includingResourceValuesForKeys:nil
relativeToURL:nil
error:&error];
if(error)
//handle error condition
else
// save your bookmark
}
[url stopAccessingSecurityScopedResource];




Again Apple's doc is confusion at best! It took me a long time to find out. Hope this helps.

+5


source


I ran into the same problem today and essentially the compiler says it is NSURLBookmarkCreationWithSecurityScope

not available on iOS.

But to my surprise, if I use the raw constant ( NSURLBookmarkCreationWithSecurityScope

maps to ( 1 << 11 )

) instead , this method seems to work. It returns a valid bookmark data object, and when I call [[NSURL URLByResolvingBookmarkData:options:relativeToURL:bookmarkDataIsStale:stale]

, the valid security is -scoped NSURL

and I can access files and directories. Also, I tested them with iCloud Drive. And the documentation only says it should work for third party document providers.



I'm not sure how reliable this approach is because it seems like Apple engineers didn't have time to finish this feature, so they disabled it at the last minute. Or it might just be a bug in the header file. If anyone knows more about this please comment.

+1


source







All Articles