Sunday, June 16, 2013

NSFileWrapper writeToURL example in Objective C (iOS).

NSFileWrapper writeToURL

Recursively writes the entire contents of a file wrapper to a given file-system URL.

- (BOOL)writeToURL:(NSURL *)url options:(NSFileWrapperWritingOptions)options originalContentsURL:(NSURL *)originalContentsURL error:(NSError **)outError

Parameters of [NSFileWrapper writeToURL]
URL of the file-system node to which the file wrapper’s contents are written.
Option flags for writing to the node located at url. See “File Wrapper Writing Options” for possible values.
The location of a previous revision of the contents being written. The default implementation of this method attempts to avoid unnecessary I/O by writing hard links to regular files instead of actually writing out their contents when the contents have not changed. The child file wrappers must return accurate values when sent the filename method for this to work. Use the NSFileWrapperWritingWithNameUpdating writing option to increase the likelihood of that.
Specify nil for this parameter if there is no earlier version of the contents or if you want to ensure that all the contents are written to files.
YES to update the receiver’s filenames (its filename and—for directory file wrappers—the filenames of its sub–file wrappers) be changed to the filenames of the corresponding nodes in the file system, after a successful write operation. Use this in Save or Save As operations.
NO to specify that the receiver’s filenames not be updated. Use this in Save To operations.
If an error occurs, upon return contains an NSError object that describes the problem. Pass NULL if you do not want error information.

Return Value of [NSFileWrapper writeToURL]
YES when the write operation is successful. If not successful, returns NO after setting outError to an NSError object that describes the reason why the file wrapper’s contents could not be written.
NSFileWrapper writeToURL example.
- (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
    NSFileWrapper *wrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil];
    [wrapper addRegularFileWithContents:[@"please work" dataUsingEncoding:NSUTF8StringEncoding] preferredFilename:@"foobar"];
    [wrapper writeToURL:absoluteURL options:NSFileWrapperWritingAtomic originalContentsURL:nil error:outError];

    NSDictionary *metadata = [NSDictionary dictionaryWithObject:@"0.1" forKey:@"Version"];
    NSURL *mdURL = [NSURL fileURLWithPath:[[absoluteURL path] stringByAppendingPathComponent:@"SiteInfo.plist"]];
    [metadata writeToURL:mdURL atomically:YES];

    return YES;

Example of [NSFileWrapper writeToURL].
NSFileManager *fileManager = [NSFileManager defaultManager];
NSFileWrapper *fw = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil];
NSArray *subpaths = [fileManager subpathsAtPath:picsPath];
for (NSString *path in subpaths) {
   NSString *longPath = [picsPath stringByAppendingPathComponent:path];
   NSString *filename = [NSString stringWithFormat:@\"%@.jpg\", [[path componentsSeparatedByString:@\".\"] objectAtIndex:0]];
   NSData *pngData = [fileManager contentsAtPath:longPath];
   UIImage *pngImage = [UIImage imageWithData:pngData];
   NSData *jpgData = UIImageJPEGRepresentation(pngImage, 1.0f);
   [fw addRegularFileWithContents:jpgData preferredFilename:filename];
NSURL *destURL = [NSURL URLWithString:[NSString stringWithFormat:@\"file://%@\",[exportPath stringByAppendingPathComponent:@'Export']]];
[fw writeToURL:destURL options:NSFileWrapperWritingAtomic originalContentsURL:nil error:nil];

End of NSFileWrapper writeToURL example article.