我试图在首次启动时将一些文件从我的应用程序包复制到文档目录。我有第一次启动的检查,但为了清楚起见,它们没有包含在代码片段中。问题是我正在复制到文档目录(已经存在)并且在文档中,它指出:
dstPath在操作之前不得存在。
我实现直接复制到文档根目录的最佳方法是什么?我想这样做的原因是允许 iTunes 文件共享支持。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Populator"];
NSLog(@"\nSource Path: %@\nDocuments Path: %@", sourcePath, documentsDirectory);
NSError *error = nil;
if([[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:documentsDirectory error:&error]){
NSLog(@"Default file successfully copied over.");
} else {
NSLog(@"Error description-%@ \n", [error localizedDescription]);
NSLog(@"Error reason-%@", [error localizedFailureReason]);
}
...
return YES;
}
谢谢