此代碼的工作
-(void) copyDirectory:(NSString *)directory {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:directory];
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:directory];
if (![fileManager fileExistsAtPath:documentDBFolderPath]) {
//Create Directory!
[fileManager createDirectoryAtPath:documentDBFolderPath withIntermediateDirectories:NO attributes:nil error:&error];
NSArray *fileList = [fileManager contentsOfDirectoryAtPath:resourceDBFolderPath error:&error];
for (NSString *s in fileList) {
NSString *newFilePath = [documentDBFolderPath stringByAppendingPathComponent:s];
NSString *oldFilePath = [resourceDBFolderPath stringByAppendingPathComponent:s];
if (![fileManager fileExistsAtPath:newFilePath]) {
//File does not exist, copy it
[fileManager copyItemAtPath:oldFilePath toPath:newFilePath error:&error];
} else {
[fileManager removeItemAtPath:newFilePath error:&error];
[fileManager copyItemAtPath:oldFilePath toPath:newFilePath error:&error];
}
}
[self info:@"Copy finish!"];
}
}
在目錄中添加文件夾的名稱複製
編輯:
您的文件夾必須通過引用加入。您的文件夾拖放到你的項目在Xcode並選擇「創建文件夾引用任何添加的文件夾」
![enter image description here](https://i.stack.imgur.com/L3dha.png)
您可以將目錄複製到imagefolder目錄中以查找目錄。你可以使用這個鏈接http://stackoverflow.com/questions/17669944/copy-folder-from-main-bundle-to-documents-directory-in-iphone –