Code snippet to copy a plist file to documents folder

This content has 13 years. Please, read this page keeping its age in your mind.

Say that you want to copy a plist file to documents folder in your iphone. The following code snippet do exactly what you want:

BOOL success;
NSError *error;

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"fakedata.plist"];
success = [fileManager fileExistsAtPath:filePath];
if (success) return;

NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingFormat:@"fakedata.plist"];
success = [fileManager copyItemAtPath:path toPath:filePath error:&error];

if (!success) {
NSAssert1(0, @"Failed to copy Plist. Error %@", [error localizedDescription]);
}

Hope to help…


	

One thought on “Code snippet to copy a plist file to documents folder

Comments are closed.