1

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.

jscs
  • 63,694
  • 13
  • 151
  • 195
RJ168
  • 1,006
  • 2
  • 12
  • 22

2 Answers2

1

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.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • Can we do this with the kaychain or icloud if yes then please tell me the way because i never done it previouly & if we done with that way so please tell me the exact storage location for file saving.. Thank you.. – RJ168 Nov 08 '11 at 09:28
  • Start reading the documentation: http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/iCloud/iCloud.html#//apple_ref/doc/uid/TP40007072-CH5-SW1 – rckoenes Nov 08 '11 at 09:30
  • Thanks i read it for a minute but with icloud, devices have to be registered there to access the data but i don't know who is gone a use my app. I will read it completely but i think i wont be work so if any other solution then please tell me.. Thank you – RJ168 Nov 08 '11 at 09:39
  • Hey i really excited about the icloud use i start reading the documentation but find it little difficult to understand can any other link help me to get understand in simple words.. Thank you. – RJ168 Nov 08 '11 at 10:24
  • 1
    Have a look in this thread:http://stackoverflow.com/questions/7795629/icloud-basics-and-code-sample – rckoenes Nov 08 '11 at 11:10
0

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.

  • ohh.. thanks but can really a problem will solved by iCloud because i never use this previously so i don't know how to do that .. If you know then please tell me how i reach that solution. And one more question if we use iCloud then what is the actual location for storing the file. Thank you – RJ168 Nov 08 '11 at 09:26