views:

291

answers:

2

Hi,

I am trying to copy a couple of image files from my MainBundle/SampleImages folder to the application NSDocumentDirectory/Library folder, but am unable to do so. I have checked that the images are in .app/Mainbundle/SampleImages directory and I have also checked that the Library folder is being created in the NSDocumentsDirectory, but no files are copied.

I have tried two approaches, given below, but with no success. here is the code that I am using to copy files.

First Method

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];     
        NSString *documentLibraryFolderPath = [documentsDirectory stringByAppendingPathComponent:@"Library"];

        NSString *resourceSampleImagesFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"SampleImages"];
        NSData *mainBundleFile = [NSData dataWithContentsOfFile:resourceSampleImagesFolderPath];
        [[NSFileManager defaultManager] createFileAtPath:documentLibraryFolderPath
                                                contents:mainBundleFile 
                                              attributes:nil];

Second Method

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];     
        NSString *documentSampleImagesFolderPath = [documentsDirectory stringByAppendingPathComponent:@"Library"];

        NSString *resourceSampleImagesFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"SampleImages/"];
        [fileManager createDirectoryAtPath: documentSampleImagesFolderPath attributes:nil];
        if([fileManager copyItemAtPath:resourceSampleImagesFolderPath toPath:documentSampleImagesFolderPath error:&error]) {
            NSLog(@"success");
        }
        else {
            NSLog(@"Failure");
            NSLog(@"%d",error);
        }

I have spent a couple of hours looking for a solution, any help here would be appreciated greatly.

Thank You

Shumais Ul Haq

A: 

I don't think you can just copy the folder itself. You'll have to copy each file in the folder individually.

Thomas Müller
From the SDK doc of -copyItemAtPath:toPath:error:,If srcPath is a directory, the method creates a new directory at dstPath and recursively populates it with duplicates of the files and directories contained in srcPath, preserving all links.
Shumais Ul Haq
A: 

I got it done with the second method by changing it bit. There is no need for creating the directory as the SDK docs say that the destination directory must not exist.

NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
                                                             NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"Library"];

        NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath]
                                              stringByAppendingPathComponent:@"SampleImages"];
            [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error];
Shumais Ul Haq