从文件夹复制文件夹(w /内容)到文档目录 – iOS

编辑:解决

谢谢布鲁克斯。 如果这个文件存在于我的软件包中,你的问题就让我继续深入研究 – 而且它并没有!

所以通过使用这个代码(也下面):iPhone / iPad的:无法复制文件夹从NSBundle NSDocumentDirectory和正确添加目录到Xcode(从这里和下面)的说明我能够得到它的工作。

将文件夹复制到Xcode中:

  1. 在Mac上创build一个目录。
  2. select添加现有文件到您的项目
  3. select您想要导入的目录
  4. 在popup窗口中,确保select“将项目复制到目标组的文件夹”和“为任何添加的文件夹创build文件夹引用”
  5. 点击“添加”
  6. 目录应该显示为蓝色而不是黄色。

    -(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]; } else { NSLog(@"Directory exists! %@", documentDBFolderPath); } 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 { NSLog(@"File exists: %@", newFilePath); } } 

    }

======================== END编辑

FRU的杂散玉树-ON! 无论如何…

下面的代码将我的文件夹从应用程序包复制到模拟器中的文档文件夹就好了。 但是在设备上,我得到一个错误,没有文件夹。 使用泽谷歌我发现错误(260)意味着文件(在这种情况下,我的文件夹)不存在。

有什么可能出错? 为什么我不能将我的文件夹从软件包复制到文档? 我检查了文件存在 – 虽然文件夹没有显示 – 因为Xcode需要平面文件? 它把我的文件夹(拖入Xcode)变成一个平面的资产文件,而不是?

我感谢你的帮助。

 // Could not copy report at path /var/mobile/Applications/3C3D7CF6-B1F0-4561-8AD7-A367C103F4D7/cmsdemo.app/plans.gallery to path /var/mobile/Applications/3C3D7CF6-B1F0-4561-8AD7-A367C103F4D7/Documents/plans.gallery. error Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x365090 {NSFilePath=/var/mobile/Applications/3C3D7CF6-B1F0-4561-8AD7-A367C103F4D7/cmsdemo.app/plans.gallery, NSUnderlyingError=0x365230 "The operation couldn’t be completed. No such file or directory"} NSString *resourceDBFolderPath; NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error; NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"plans.gallery"]; BOOL success = [fileManager fileExistsAtPath:documentDBFolderPath]; if (success){ NSLog(@"Success!"); return; } else { resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"plans.gallery"]; [fileManager createDirectoryAtPath: documentDBFolderPath attributes:nil]; //[fileManager createDirectoryAtURL:documentDBFolderPath withIntermediateDirectories:YES attributes:nil error:nil]; [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error]; } //check if destinationFolder exists if ([ fileManager fileExistsAtPath:documentDBFolderPath]) { //removing destination, so source may be copied if (![fileManager removeItemAtPath:documentDBFolderPath error:&error]) { NSLog(@"Could not remove old files. Error:%@",error); return; } } error = nil; //copying destination if ( !( [ fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error ]) ) { NSLog(@"Could not copy report at path %@ to path %@. error %@",resourceDBFolderPath, documentDBFolderPath, error); return ; } 

我冒昧地编辑了一些我觉得需要一点点家务的代码。 你正在使用不推荐的方法,过于复杂的方法,只是简单的荒谬,如果 – elses。 我当然会检查你的文件path是否有效,不知道.gallery文件是什么,并且不要试图提供一个虚拟文件,我唯一能做的判断是你的文件path是无效的,因为资源不会不存在你认为它的地方。 (一次,你要求将文件复制到文档目录中,然后检查它是否存在于你的包中!)

  -(void)testMethod { NSString *resourceDBFolderPath; NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error; NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"plans.gallery"]; BOOL success = [fileManager fileExistsAtPath:documentDBFolderPath]; if (success){ NSLog(@"Success!"); return; } else { //simplified method with more common and helpful method resourceDBFolderPath = [[NSBundle mainBundle] pathForResource:@"plans" ofType:@"gallery"]; //fixed a deprecated method [fileManager createDirectoryAtPath:documentDBFolderPath withIntermediateDirectories:NO attributes:nil error:nil]; [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error]; //check if destinationFolder exists if ([ fileManager fileExistsAtPath:documentDBFolderPath]) { //FIXED, another method that doesn't return a boolean. check for error instead if (error) { //NSLog first error from copyitemAtPath NSLog(@"Could not remove old files. Error:%@", [error localizedDescription]); //remove file path and NSLog error if it exists. [fileManager removeItemAtPath:documentDBFolderPath error:&error]; NSLog(@"Could not remove old files. Error:%@", [error localizedDescription]); return; } } } }