Sunday, April 21, 2013

NSFileManager copyItemAtURL example ios


copyItemAtURL :toURL:error:

Copies the file at the specified URL to a new location synchronously.
- (BOOL)copyItemAtURL:(NSURL *)srcURL toURL:(NSURL *)dstURL error:(NSError **)error
Parameters
srcURL
The file URL that identifies the file you want to copy. The URL in this parameter must not be a file reference URL. This parameter must not be nil.
dstURL
The URL at which to place the copy of srcURL. The URL in this parameter must not be a file reference URL and must include the name of the file in its new location. This parameter must not be nil.
error
On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.
Return Value of [NSFileManager copyItemAtURL]
YES if the item was copied successfully or the file manager’s delegate aborted the operation deliberately. Returns NO if an error occurred.
Discussion of [NSFileManager copyItemAtURL]
When copying items, the current process must have permission to read the file or directory at srcURL and write the parent directory of dstURL. If the item at srcURL is a directory, this method copies the directory and all of its contents, including any hidden files. If a file with the same name already exists at dstURL, this method aborts the copy attempt and returns an appropriate error. If the last component of srcURL is a symbolic link, only the link is copied to the new path.
Prior to copying each item, the file manager asks its delegate if it should actually do so. It does this by calling the fileManager:shouldCopyItemAtURL:toURL: method; if that method is not implemented (or the process is running in OS X 10.5 or earlier) it calls the fileManager:shouldCopyItemAtPath:toPath:method instead. If the delegate method returns YES, or if the delegate does not implement the appropriate methods, the file manager proceeds to copy the file or directory. If there is an error copying an item, the file manager may also call the delegate’sfileManager:shouldProceedAfterError:copyingItemAtURL:toURL: orfileManager:shouldProceedAfterError:copyingItemAtPath:toPath: method to determine how to proceed.
Example of [NSFileManager copyItemAtURL]

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreData.sqlite"];
NSURL *initialURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"CoreData" ofType:@"sqlite"]];

NSError *error = nil;

if (![[NSFileManager defaultManager] fileExistsAtPath:[initialURL absoluteString]]) {
    NSLog(@"Original does not exist. \nPath: %@", [initialURL absoluteString]);
}  

if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL absoluteString]]) {
    NSLog(@"Destination file does not exist. \nPath: %@", [storeURL absoluteString]);

    [[NSFileManager defaultManager] copyItemAtURL:initialURL toURL:storeURL error:&error];

    NSLog(@"Error: %@", [error description]);
}