Question

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.

Was it helpful?

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.

OTHER TIPS

Your use case can't be supported by writing to a shared folder because there is no such facility in iOS. However, your high-level goal (having a file available to the app even after it's been removed and reinstalled) could be serviced using iCloud, so have a look at syncing the file over iCloud instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top