tags:

views:

40

answers:

1

I'm in trouble with paths, relative paths, NSBundle and all the path/file related operations :)

While i run the simulator everthing goes right but all the file changes are not permanent, so everytime i run my app i have to repeat the initial setup of my app.

The question: What is the proper way to read and write files (from resource dir) and make all the file changes permanent (updated into the project folder) ?

Thanks

EDIT: i write a simple method to do it, it's correct ?

-(NSString *) getPath:(NSString *)forResource
{
    NSString *pathRes, *docPath, *destPath;
    NSArray *foundPaths;
    NSFileManager *fs = [[NSFileManager alloc] init];

    foundPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    docPath = [foundPaths objectAtIndex:0];
    destPath = [[NSString stringWithFormat:@"%@/%@",docPath,forResource] stringByStandardizingPath];


    if(![fs fileExistsAtPath:destPath])
    {
        pathRes = [[NSBundle mainBundle] pathForResource:forResource ofType:nil];
        [fs copyItemAtPath:pathRes toPath:destPath error:nil];
    }

    return destPath;
}
+2  A: 

You cannot write to the application folder on the device. Write any persistent data to your documents directory. You can copy files from the resources directory to the documents directory on the first run to keep your existing logic.

NSSearchPathForDirectoriesInDomains( NSDocumentDirectory , NSUserDomainMask , YES );
drawnonward
haaaa, so "the bundle" it's the .app file and the "resource" directory it's inside it but it's read only... it's correct ?
Cesar
Yes. It is bad form for an application to modify itself, and the iPhoneOS enforces that.
drawnonward
Thanks for the answer, now it's much more clear how it works. It correct the small method that i posted up into the question ?
Cesar
Your method looks correct. You should use `[docPath stringByAppendingPathComponent:forPath]` instead of stringWithFormat, even though they will have the same result.
drawnonward