Tuesday, April 30, 2013

NSURL writeBookmarkData example ios


writeBookmarkData :toURL:options:error:

Creates an alias file on disk at a specified location with specified bookmark data.
+ (BOOL)writeBookmarkData:(NSData *)bookmarkData toURL:(NSURL *)bookmarkFileURLoptions:(NSURLBookmarkFileCreationOptions)options error:(NSError **)error
Parameters of [NSURL writeBookmarkData]
bookmarkData
The bookmark data containing information for the alias file.
bookmarkFileURL
The desired location of the alias file.
options
Options taken into account when creating the alias file.[NSURL writeBookmarkData]
error
The error that occurred in the case that the alias file cannot be created.
Return Value
YES if the alias file is successfully created; otherwise, NO.
Discussion of [NSURL writeBookmarkData]
This method will produce an error if bookmarkData was not created with theNSURLBookmarkCreationSuitableForBookmarkFile option.
If bookmarkFileURL points to a directory, the alias file will be created in that directory with its name derived from the information in bookmarkData. If bookmarkFileURL points to a file, the alias file will be created with the location and name indicated by bookmarkFileURL, and its extension will be changed to .alias if it is not already.
Example of [NSURL writeBookmarkData]

NSURL *url = [NSURL fileURLWithPath:pathToAliasTarget];
NSError *err = nil;
NSData *bookmarkData = [url bookmarkDataWithOptions: NSURLBookmarkCreationSuitableForBookmarkFile includingResourceValuesForKeys:nil relativeToURL:nil error:&err];

if(bookmarkData == nil) {
  //handle NSError in err
} else {
  if(![NSURL writeBookmarkData :bookmarkData toURL:aliasFileURL options:NSURLBookmarkCreationSuitableForBookmarkFile error:&err]) {
    //handle NSError in err
  }
}