将文件夹(w/contents)从bundle复制到Documents目录- iOS

时间:2021-11-16 00:32:37

EDIT: SOLVED

Thanks *s. Your question led me to keep digging into if the file even existed in my bundle - and it did not!

谢谢布鲁克斯。你的问题让我不断地探究这个文件是否存在于我的包中——而事实并非如此!

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.

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

Copy folder into Xcode:

文件夹复制到Xcode:

  1. Create a Directory on your Mac.
  2. 在Mac上创建一个目录。
  3. Select Add Existing Files to your project
  4. 选择添加现有文件到您的项目。
  5. Select the Directory you want to import
  6. 选择要导入的目录
  7. In the pop-up window make sure you select "Copy items into destination group's folder" and "Create Folder References for any added folders"
  8. 在弹出窗口中,确保选择“将项目复制到目标组的文件夹中”和“为任何添加的文件夹创建文件夹引用”
  9. Hit "Add"
  10. 点击“添加”
  11. 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

= = = = = = = = = = = = = = = = = = = = = = = =结束编辑

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

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

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.

下面的代码将我的文件夹从app bundle中复制到模拟器中的Documents文件夹。但是在设备上我得到一个错误和没有文件夹。使用ze谷歌,我发现错误(260)表示文件(在本例中是我的文件夹)不存在。

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?

出什么问题了?为什么我不能将文件夹从包中复制到文档中呢?我检查了文件是否存在——尽管文件夹没有显示——因为Xcode需要平面文件?它是否将我的文件夹(拖到Xcode中)变成了资产的平面文件?

I thank you for any assistance.

谢谢你的帮助。

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

1 个解决方案

#1


10  

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!)

我冒昧地编辑了一些我认为需要进行一些管理的代码。您使用的是不赞成的方法、过于复杂的方法,以及非常荒谬的if-elses。我当然会检查你的文件路径是有效的,没有知道.gallery文件,并没有试图提供一个虚拟的,我可以是你的唯一判断文件路径是无效的,因为资源不存在,你认为它是我的。(在某一点,您要求将文件复制到文档目录中,然后检查它是否存在于您的包中!)

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

#1


10  

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!)

我冒昧地编辑了一些我认为需要进行一些管理的代码。您使用的是不赞成的方法、过于复杂的方法,以及非常荒谬的if-elses。我当然会检查你的文件路径是有效的,没有知道.gallery文件,并没有试图提供一个虚拟的,我可以是你的唯一判断文件路径是无效的,因为资源不存在,你认为它是我的。(在某一点,您要求将文件复制到文档目录中,然后检查它是否存在于您的包中!)

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