Search code examples
objective-cioscocoa-touchnsfilemanagerfile-management

Save a file on device that will survive uninstallation


I want to save a .txt file in my iPhone app, but I want it to persist such that I can still access it even if the app is uninstalled and reinstalled.

I have tried to save the file on the iPhone's hard disk but it never works.

This works on the simulator:

NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];


//NSString *dataFile= @"/Users/rajesh/Library/Application Support/iPhone Simulator/4.2/tmp/myfile.dat"; //on simulator
NSData *databuffer;
databuffer = [textBox.text dataUsingEncoding: NSASCIIStringEncoding];
[filemgr createFileAtPath: dataFile contents: databuffer attributes:nil];
[filemgr release];

And I use this on the device:

NSString *dataFile= @"/var/mobile/Applications/temp/myfile.dat"; //on device

but it's not working.


Solution

  • There is no way the do this, since all files are in a sandbox which is part of your application and you can't write files outside of the sandbox.

    You could try saving the file in the keychain (dirty hack) or storing it via iCloud.