Question

I want to copy the database file from bundle to user document.

My code is below:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *userPath = [documentsDir stringByAppendingPathComponent:@"db.sql"];

NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"db.sql"];

NSFileManager *fileManager = [NSFileManager defaultManager];

NSLog(@"Bundle database exists: %i",[fileManager fileExistsAtPath:srcPath]);
NSLog(@"User Document folder database exists: %i",[fileManager fileExistsAtPath:userPath]);


BOOL find = [fileManager fileExistsAtPath:userPath];
BOOL copySuccess = FALSE;

NSError *error;

if (!find) {
    NSLog(@"don't have writable copy, need to create one");
    copySuccess = [fileManager copyItemAtPath:srcPath toPath:userPath error:&error];
}

if (!copySuccess) {

    NSLog(@"Failed with message: '%@'.",[error localizedDescription]);
}

and the result is always saying:

Bundle database exists: 1 User Document folder database exists: 0
don't have writable copy, need to create one Failed with message: 'The
operation couldn’t be completed. (Cocoa error 4.)'.

Please suggest, thanks.

Was it helpful?

Solution

Your code for determining your user's Documents Directory is incorrect.

Using your code, I put together a quick and dirty sample that works. For your application, you probably want to create some utils class that contains the static function 'applicationDocumentsDirectory' so that other classes in your project can call it, if needed.

Header File:

#import <UIKit/UIKit.h>

@interface TST_ViewController : UIViewController

+ (NSString *) applicationDocumentsDirectory;

@end

Implementation File:

#import "TST_ViewController.h"

@interface TST_ViewController ()

@end

@implementation TST_ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *dbFilename = @"db.sql";
    NSString *userPath = [[TST_ViewController applicationDocumentsDirectory] stringByAppendingPathComponent:dbFilename];
    NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbFilename];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    BOOL foundInBundle = [fileManager fileExistsAtPath:srcPath];
    BOOL foundInUserPath = [fileManager fileExistsAtPath:userPath];
    BOOL copySuccess = FALSE;

    NSError *error;

    if(foundInBundle) {

        if (!foundInUserPath) {
            NSLog(@"Don't have a writable copy, so need to create one...");
            copySuccess = [fileManager copyItemAtPath:srcPath toPath:userPath error:&error];
        }

        if (!copySuccess) {
            NSLog(@"Failed with message: '%@'.",[error localizedDescription]);
        }

    } else {
        // handle error in the event the file is not included in the bundle
    }

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

+ (NSString *) applicationDocumentsDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top