无法将文件夹从应用程序包复制到文档目录

时间:2014-02-25 11:56:41

标签: ios iphone nsfilemanager

我正在尝试将文件夹从应用程序包复制到iphone文档目录,但它没有发生。

我已使用此代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Photos"];


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

    NSError *error1;
    NSArray *resContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:sourcePath error: &error1];
    if (error1) {
        NSLog(@"error1 : %@",error1);
    }
    [resContents enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
     {
         NSError* error;
         if (![[NSFileManager defaultManager]
               copyItemAtPath:[sourcePath stringByAppendingPathComponent:obj]
               toPath:[documentsDirectory stringByAppendingPathComponent:obj]
               error:&error])
             NSLog(@"%@", [error localizedDescription]);
     }];
}

并尝试了这种方法

- (void)viewDidLoad{
     [self copyFolder];
    }

- (void) copyFolder {
    BOOL success1;
    NSFileManager *fileManager1 = [NSFileManager defaultManager];
    fileManager1.delegate = self;
    NSError *error1;
    NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory1 = [paths1 objectAtIndex:0];
    NSString *writableDBPath1 = [documentsDirectory1 stringByAppendingPathComponent:@"/Photos"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:writableDBPath1])
        [[NSFileManager defaultManager] createDirectoryAtPath:writableDBPath1 withIntermediateDirectories:NO attributes:nil error:&error1];

    NSString *defaultDBPath1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Photos"];
    NSLog(@"default path %@",defaultDBPath1);
    success1 = [fileManager1 copyItemAtPath:defaultDBPath1 toPath:writableDBPath1 error:&error1];
    if (!success1 )
    {
        NSLog(@"error1 : %@",error1);
    } else {
    }
}

- (BOOL)fileManager:(NSFileManager *)fileManager shouldProceedAfterError:(NSError *)error copyingItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath{
    if ([error code] == 516) //error code for: The operation couldn’t be completed. File exists
        return YES;
    else
        return NO;
}

显示此错误

  

错误Domain = NSCocoaErrorDomain Code = 260“操作无法完成。(Cocoa错误260.)”UserInfo = 0x8a39800 {NSUnderlyingError = 0x8a385b0“操作无法完成。没有这样的文件或目录”,NSFilePath = / Users / user / Library / Application Support / iPhone Simulator / 7.0.3 / Applications / FC7F93EE-CFB7-41CF-975B-2E3B18760202 / app.app / Photos,NSUserStringVariant =(       夹   )}

但这是文件夹

enter image description here

4 个答案:

答案 0 :(得分:4)

照片似乎是一个群组,而不是文件夹参考。因此,所有图像photo1.jpg等都直接存储在您的资源包下。

路径<resource_bundle>/Photos不存在。要验证这一点,请转到应用.app

.app->Right-click->Show Package Contents

不是将照片作为一个组添加,而是将其添加为文件夹参考,并在目标复制包资源下,确保添加整个文件夹。

希望有所帮助!

答案 1 :(得分:1)

您的照片不在照片目录中。它们仅存在于/Users/devubhamanek/Library/Application Support/iPhone Simulator/7.0.3/Applications/FC7F93EE-CFB7-41CF-975B-2E3B18760202/MTPhotoViewer.app//Users/devubhamanek/Library/Application Support/iPhone Simulator/7.0.3/Applications/FC7F93EE-CFB7-41CF-975B-2E3B18760202/MTPhotoViewer.app/photo1.jpg

如果要在bundle中创建目录,则应在Photos目录中添加如下文件: enter image description here

项目导航器中的文件夹应为:enter image description here

答案 2 :(得分:1)

确保您的文件夹存在。 您可以轻松地将XCode中的论坛分配到物理文件夹

点击group,您将在右侧窗格中找到位置设置。

folder符号选择物理文件夹location

实际上,这对于保持项目结构清洁(不仅在XCode项目中)而且在物理上在磁盘上非常有用。

enter image description here

答案 3 :(得分:1)

Swift 3完整解决方案代码 注意:使用以下方法创建带参考文件夹后。

linkwrap