2
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;
}


NSString *dbFilename = @"CoordinatesDatabase.sql";
NSString *userPath = [[CoordinatesAppDelegate applicationDocumentsDirectory] stringByAppendingPathComponent: dbFilename];
NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: dbFilename];
NSLog(@"userPath %@", userPath);
NSLog(@"srcPath %@", srcPath);
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 copy, copy one");
        copySuccess = [fileManager copyItemAtPath: srcPath toPath: userPath error: &error];
    }
    if(!copySuccess)
    {
        NSLog(@"Failed with message: %@'.", [error localizedDescription]);
    }
}
else
{
    NSLog(@"Some error");
}
return YES;
}

According to foundInBundle, the CoordinatesDatabase.sql exists in the application bundle. Why can't the "copySuccess = [fileManager copyItemAtPath: srcPath toPath: userPath error: &error];" be successful?

The error it produces is something like "Operation couldn't be completed. Cocoa Error 4". According to my googling, Cocoa Code Error 4 means NSFileNoSuchFileError but I'm really sure that the CoordinatesDatabase.sql is there in the app bundle.

Or am I wrong that CoordintesDatabase.sql does exist in the app bundle?

John Conde
  • 217,595
  • 99
  • 455
  • 496
Tooth Less
  • 21
  • 2

1 Answers1

0

You can't create, delete, or change files that are in your app bundle. You have to write your files out to the proper documents or cache folder. And that is a little complicated as well because the rules changed between iOS4 and iOS5.

Also your app can get rejected for creating files in the wrong folders. I ended up writing a method to handle the creation of a documents folder depending on what OS version they're running... and it supports migrating the data from the old folder to the new one when the user upgrades the os in the phone. You should do the same.

I suggest you look at the other questions in here that deal with this issue: iOS PhoneGap app rejected because of the use of localStorage

Community
  • 1
  • 1
badweasel
  • 2,349
  • 1
  • 19
  • 31