Tuesday, April 30, 2013

NSURL URLByResolvingBookmarkData example ios

URLByResolvingBookmarkData :options:relativeToURL:bookmarkDataIsStale:error:

Returns a new URL made by resolving bookmark data.
+ (id)URLByResolvingBookmarkData:(NSData *)bookmarkData options:(NSURLBookmarkResolutionOptions)options relativeToURL:(NSURL *)relativeURLbookmarkDataIsStale:(BOOL *)isStale error:(NSError **)error
Parameters of [NSURL URLByResolvingBookmarkData]
bookmarkData
The bookmark data the URL is derived from.
options
Options taken into account when resolving the bookmark data.
To resolve a security-scoped bookmark to support App Sandbox, you must include (by way of bitwise OR operators with any other options in this parameter) theNSURLBookmarkResolutionWithSecurityScope option.[NSURL URLByResolvingBookmarkData]
relativeURL
The base URL that the bookmark data is relative to.
If you are resolving a security-scoped bookmark to obtain a security-scoped URL, use this parameter as follows:
  • To resolve an app-scoped bookmark, use a value of nil.
  • To resolve a document-scoped bookmark, use the absolute path (despite this parameter’s name) to the document from which you retrieved the bookmark.
isStale
If YES, the bookmark data is stale.
error
The error that occurred in the case that the URL cannot be created.
Return Value of [NSURL URLByResolvingBookmarkData]
A new URL made by resolving bookmarkData.
Discussion
To obtain a security-scoped URL from a security-scoped bookmark, call this method using theNSURLBookmarkResolutionWithSecurityScope option. In addition, to use security scope, you must first have enabled the appropriate entitlements for your app, as described in “Enabling Security-Scoped Bookmark and URL Access” in Entitlement Key Reference.
To then obtain access to the file-system resource pointed to by a security-scoped URL (in other words, to bring the resource into your app’s sandbox), call thestartAccessingSecurityScopedResource method (or its Core Foundation equivalent) on the URL.[NSURL URLByResolvingBookmarkData]
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.
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.

Example of [NSURL URLByResolvingBookmarkData]
- (NSData *)bookmarkFromURL:(NSURL *)url {
    NSData *bookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationMinimalBookmark
                     includingResourceValuesForKeys:NULL
                                      relativeToURL:NULL
                                              error:NULL];
    return bookmark;
}

- (NSURL *)urlFromBookmark:(NSData *)bookmark {
    NSURL *url = [NSURL URLByResolvingBookmarkData:bookmark
                                           options:NSURLBookmarkResolutionWithoutUI
                                     relativeToURL:NULL
                               bookmarkDataIsStale:NO
                                             error:NULL];
    return url;
}