将文件夹(w /内容)从捆绑包复制到文档目录 - iOS [英] Copy Folder (w/contents) from bundle to Documents directory - iOS

查看:435
本文介绍了将文件夹(w /内容)从捆绑包复制到文档目录 - iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢Brooks。

因此,通过使用这个代码(下面也是如此):iPhone / iPad :无法将文件夹从NSBundle复制到NSDocumentDirectory以及将目录正确添加到Xcode的说明(从

So by using this code (also below): iPhone/iPad: Unable to copy folder from NSBundle to NSDocumentDirectory and the instructions for adding a directory properly to Xcode (from here and below)I was able to get it to work.

将文件夹复制到Xcode中:

Copy folder into Xcode:


  1. 在Mac上创建目录。

  2. 选择向您的项目添加现有文件

  3. 选择要导入的目录

  4. 在弹出窗口中,确保选择将项目复制到
    目标组的文件夹任何
    添加的文件夹

  5. 点击添加

  6. 目录应显示蓝色而不是黄色。

  1. Create a Directory on your Mac.
  2. Select Add Existing Files to your project
  3. Select the Directory you want to import
  4. In the pop-up window make sure you select "Copy items into destination group's folder" and "Create Folder References for any added folders"
  5. Hit "Add"
  6. The Directory should appear blue instead of yellow.

-(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 EDIT

======================== END EDIT

Frus-stray-shee-on!无论如何...

Frus-stray-shee-on! Anyway...

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

The code below copies my folder from the app bundle to the Documents folder in the simulator just fine. However on the device I get an error and no folder. Using ze Google I found out that the error (260) means the file (in this case my folder) does not exist.

可能会出错?为什么我不能将我的文件夹从捆绑包复制到文档?我检查了文件存在 - 虽然文件夹不显示 - 因为Xcode想要平面文件?是否将我的文件夹(拖入Xcode)转换为资产的平面文件?

What could be going wrong? Why can I not copy my folder from the bundle to the Documents? I have checked that the files exist - although the folder is not showing up - because Xcode wants flat file? Did it turn my folder (dragged into Xcode) into a flat file of assets instead?

感谢您的任何帮助。

//  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 ;
}


推荐答案

的编辑一些代码我觉得需要一点家政。你使用的是过时的方法,过于复杂的方法,只是一些荒谬的if-elses。我当然会检查你的文件路径是有效的,不知道.gallery文件是什么,并没有尝试提供一个假的,我可以做的唯一的判断是,你的文件路径是简单无效,因为资源不不存在你认为它在哪里。 (在某一时刻,您要求将文件复制到文档目录,然后检查它是否存在于您的包中!)

I've taken the liberty of editing some of the code I feel needs a little housekeeping. You're using deprecated methods, overly-complicated methods, and just plain ridiculous if-elses. I would of course check that your file path is valid, having no idea what a .gallery file is, and making no attempt to provide a dummy one, the only judgement I can make is that your file path is simply invalid because the resource doesn't exist where you think it does. (At one point, you ask to copy the file into the documents directory, then check if it exists in your bundle!)

    -(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;
            }
        }
    }
}

这篇关于将文件夹(w /内容)从捆绑包复制到文档目录 - iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆