Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to copy folder from application bundle to document directory

I am trying to copy a folder from the application bundle to iphone document directory but it isnt happening.

I have used this codes

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

And also tried this approach

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

Its display this error

Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x8a39800 {NSUnderlyingError=0x8a385b0 "The operation couldn’t be completed. No such file or directory", NSFilePath=/Users/user/Library/Application Support/iPhone Simulator/7.0.3/Applications/FC7F93EE-CFB7-41CF-975B-2E3B18760202/app.app/Photos, NSUserStringVariant=( Folder )}

But Here is the folder

enter image description here

like image 304
Dilip Avatar asked Feb 25 '14 11:02

Dilip


2 Answers

Photos seems like a group and not a folder reference. Hence all the images photo1.jpg.. etc are stored directly under your resource bundle.

The path <resource_bundle>/Photos does not exists. To verify this go to the application .app

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

Instead of adding Photos as a group, add it as Folder reference and under your target Copy Bundle Resources make sure the entire folder is added.

Hope that helps!

like image 136
Amar Avatar answered Oct 10 '22 05:10

Amar


Your photos is not in the Photos directory. They just exist in /Users/devubhamanek/Library/Application Support/iPhone Simulator/7.0.3/Applications/FC7F93EE-CFB7-41CF-975B-2E3B18760202/MTPhotoViewer.app/ like /Users/devubhamanek/Library/Application Support/iPhone Simulator/7.0.3/Applications/FC7F93EE-CFB7-41CF-975B-2E3B18760202/MTPhotoViewer.app/photo1.jpg

If you want to create an directory in bundle, you should add the files in Photos directory like this: enter image description here.

The folder in your project navigator should be:enter image description here

like image 30
KudoCC Avatar answered Oct 10 '22 04:10

KudoCC