View Full Version : Storing files on iphone

Nov 25, 2008, 04:51 AM
Hello, i need some xml files stored on iphone, is there any way i can include them to my app so they are available on iphone when the user installs my iApp. I know i can add images to project and then load them with +imageName
function but i need xml files and to be in specific directory. thx

Nov 25, 2008, 05:42 AM
You can add any files you like to the resources of your application bundle and get access to the using the NSBundle methods as described in the documentation.

Nov 25, 2008, 06:28 AM
thx, there are two guide on apple for this actually, Bundle Programming Guide and Resource programming guide but i didnt find it with google.
For all n00bs like me, the solution is very simple:

// Get "News.xml" path from bundle
NSString *filePath=[[NSBundle mainBundle] pathForResource:@"News" ofType:@"xml"];

this way you get file path which you can use in standard file routines.

Nov 25, 2008, 06:44 AM
One thing you should be aware of is that you can't change files within your bundle (breaks the code signing). If you are intending updating that file copy it to your applications sandbox area on first run...

Nov 25, 2008, 07:29 AM
Yeah,you can't change the file in your bundle but you can access it. If you need to write files you need to locate your applications directory and most objects have a write function.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"test.txt"];
[myArray writeToFile:documentsDirectory atomically:NO];