Code snippet to copy a plist file to documents folder

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

Leave a Reply

Your email address will not be published. Required fields are marked *