Tuesday, April 30, 2013

NSURL bookmarkDataWithOptions example ios


bookmarkDataWithOptions :includingResourceValuesForKeys:relativeToURL:error:

Returns a bookmark for the URL, created with specified options and resource values.
- (NSData *)bookmarkDataWithOptions:(NSURLBookmarkCreationOptions)optionsincludingResourceValuesForKeys:(NSArray *)keys relativeToURL:(NSURL *)relativeURL error:(NSError **)error
Parameters of [NSURL bookmarkDataWithOptions]
options
Options taken into account when creating the bookmark for the URL.
To create a security-scoped bookmark to support App Sandbox, include (by way of bitwise ORoperators with any other options in this parameter) theNSURLBookmarkCreationWithSecurityScope option.[NSURL bookmarkDataWithOptions]
When you later resolve the bookmark, you can use the resulting security-scoped URL to obtain read/write access to the file-system resource pointed to by the URL.
If you instead want to create a security-scoped bookmark that, when resolved, enables you to obtain read-only access to a file-system resource, bitwise OR this parameter’s value with both the NSURLBookmarkCreationWithSecurityScope option and theNSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess option.[NSURL bookmarkDataWithOptions]
keys
An array of names of URL resource properties.
relativeURL
The URL that the bookmark data will be relative to.
If you are creating a security-scoped bookmark to support App Sandbox, use this parameter as follows:[NSURL bookmarkDataWithOptions]
  • To create an app-scoped bookmark, use a value of nil.
  • To create a document-scoped bookmark, use the absolute path (despite this parameter’s name) to the document file that is to own the new security-scoped bookmark.
error
The error that occurred in the case that the bookmark data cannot be created.
Return Value
A bookmark for the URL.
Discussion of [NSURL bookmarkDataWithOptions]
To use this method to create a security-scoped bookmark to support App Sandbox, 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. In addition, be sure to understand the behavior of the options and relativeURL parameters.[NSURL bookmarkDataWithOptions]
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.
Example of [NSURL bookmarkDataWithOptions]
- (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;
}