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.