views:

259

answers:

3

I have multiple UITextViews with corresponding .txt files. I'm reading them with NSString's

stringWithContentsOfFile

but I don't know the path where I should put my files. If I put it to /tmp/ on my Mac, it works in Simulator, but, of course, doesn't work on the actual device. So where should I put the files, so they'll work on both Simulator and actual Device.

Thanks in advance!

A: 

Place them into Documents folder under your application structure. You can get actual path to it by calling:

NSString *docsDirPath = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents"];

This will work on the device as well as in the Simulator.

Matthes
+2  A: 

Add them as resources to your project and you'll be able to load them from your application bundle using path:

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"filename" 
                                                            ofType:@"txt"];

If you want to make these files editable, though, you can keep them in Documents folder in application sandbox. To get its path you can use:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                        NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

So workflow in this case may be the following:

  1. Check if required file is present in documents folder. If no - copy it there from resources.
  2. Read data from file (from Documents folder)
  3. Save updated data to file (in Documents folder) if needed
Vladimir
Thanks, it worked!
Knodel
A: 

You'll want either NSTemporaryDirectory or NSDocumentDirectory.

An in-depth example for using UITextView with files can be found at http://servin.com/iphone/iPhone-File-IO.html

MHarrison