views:

286

answers:

1

Hi,

In my app I want to copy a file from my bundle to documents directory in order to modify it.

Here's my code:

+(BOOL) copyDB: (NSString*) pdbName {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *dbPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES) objectAtIndex: 0];
    dbPath = [dbPath stringByAppendingPathComponent: @"myApp"];
    dbPath = [dbPath stringByAppendingPathComponent: @"Profiles"];
    dbPath = [dbPath stringByAppendingPathComponent: @"ES-EN"];

    dbPath = [dbPath stringByAppendingPathExtension: @"prof"];

    if([fileManager fileExistsAtPath: dbPath]) {
     DebugLog(@"file exists at path %@", dbPath);
     return FALSE;
    }

    NSString *defaultDBPath = [ [ [NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"ES-EN"];
    defaultDBPath = [defaultDBPath stringByAppendingPathExtension: @"prof"];

    DebugLog(@"dbPath: %@", dbPath);
    DebugLog(@"defaultDBPath: %@", defaultDBPath);

    if(![fileManager fileExistsAtPath: defaultDBPath]) {
     DebugLog(@"Cannot find resource file");
     return FALSE;
    }

    BOOL success = [fileManager copyItemAtPath: defaultDBPath toPath: dbPath error: &error];

    if (!success) {
     DebugLog(@"!success: Failed to create writable database file with message '%@'.", [error localizedDescription]);
     UIAlertView *msg = [[UIAlertView alloc]
          initWithTitle:@"Error"
          message: [NSString stringWithFormat: @"Failed to create writable database file with message '%@'.", [error localizedDescription] ]
          delegate:self cancelButtonTitle:@"OK"
          otherButtonTitles:nil, nil];
     [msg show];
     [msg release];
     return FALSE;
    }

    [ [UserProfile sharedUserProfile] writeInitialData: pdbName];

    //Creating fruits folder
    NSString *dicDir = [Fruits_Dir stringByAppendingPathComponent: pdbName];

    //Does a directory exist?
    if ( ![fileManager fileExistsAtPath: dicDir] )
    {
     //Create a directory
     DebugLog(@"Creating userName dir");
     if (![fileManager createDirectoryAtPath: dicDir attributes:nil])
     {
      DebugLog(@"failed to create dicDir");
     }
    }
    return TRUE;
}

I suppose that this code should work. Somehow it works as it should on the simulator, but it doesn't work on the device. I get a log that the resource file cannot be found.

What may be the problem?

Thanks.

+1  A: 

One way that this can happen is for the resource file to have been accidentally removed as a reference from your XCode project.

If the file was once there then likely it has been installed into the simulator app directory. Even after removing it from XCode the file will stay there and your app will work in the simulator.

But as soon as you install it clean on the device the file will be missing.

Confirm that the file is listed in your XCode project resources and completely remove the app from the simulator and try it there again to get everything in sync.

Paul Robinson
I've rebuilded the app with "release" configuration instead of "debug" and this time all resources seem to be copied as they should be.How comes? :)
Ilya
When building an app, all the resources are copied in a phase called (appropriately enough) "Copy Resources". Check that build phase on the broken configurations and ensure your resources are part of the phase.
Tim