How to move plist from package to document folder on iOS

So, all I want to know is how to transfer plist from the package to the document folder when I first run the application, because I need it. Please, I can’t find out how to do this, so help me.

+3
source share
3 answers

If your plist name is "friends"

just use the code below (it will find if the file exists or not and copy)

NSFileManager *fileManger=[NSFileManager defaultManager];
NSError *error;
NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];

NSString *destinationPath= [doumentDirectoryPath stringByAppendingPathComponent:@"friends.plist"];

NSLog(@"plist path %@",destinationPath);    
if ([fileManger fileExistsAtPath:destinationPath]){
    //NSLog(@"database localtion %@",destinationPath);
    return;
}
NSString *sourcePath=[[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"friends.plist:];

[fileManger copyItemAtPath:sourcePath toPath:destinationPath error:&error];

which will copy plist from resources to the document directory

+4
source

This function should do the trick.

- (void)copyPlist {
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"myplist" ofType:@"plist"];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    [[NSFileManager defaultManager] copyItemAtPath:plistPath toPath:documentsDirectory error:nil];
}

You do not need to use the NSError argument in the string [[NSFileManager defaultManager] copyItemAtPath:plistPath toPath:documentsDirectory error:nil];, but it can be useful for debugging.

+2
source

Just heads-up ... it came from http://samsoff.es/posts/iphone-plist-tutorial "Using JSON is extremely preferable to Plists these days since JSON parsers have come a long way in speed: they're actually actually faster than binary slabs (now nuts). In any case, please, please do not use plists for your API. Generating them is slow and unreliable. You must use JSON. "

0
source

All Articles