我想知道如何在应用程序第一次运行时将plist文件从bundle移动到文档文件夹,因为我需要它。但我不知道如何操作,请帮助我。
我想知道如何在应用程序第一次运行时将plist文件从bundle移动到文档文件夹,因为我需要它。但我不知道如何操作,请帮助我。
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];
- (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];
}
在[[NSFileManager defaultManager] copyItemAtPath:plistPath toPath:documentsDirectory error:nil];
这一行中,您不必须使用NSError参数,但它可能对调试有用。
提醒一下...这段话来自http://samsoff.es/posts/iphone-plist-tutorial“现在使用JSON比Plists更受欢迎,因为JSON解析器的速度已经大大提高。它们实际上比二进制plist还要快(这太疯狂了)。无论如何,请不要使用plists作为您的API。生成它们很慢且不可靠。您应该使用JSON。就是这样。”