views:

30

answers:

1

What is the correct filepath to store a plist on the device.

I read this in a tutorial

which will work for the computer, would like to know the file location for the device.

// write xml representation of dictionary to a file
[dictionary writeToFile:@"/Users/henrik/Sites/foo.plist" atomically:NO];

I currently try to write to the filepath that I read from

NSString *filepath = [[NSBundle mainBundle] 
                      pathForResource:@"UserAdded" ofType:@"plist"];
userAddedQuotes = [[NSMutableArray alloc] initWithContentsOfFile:filepath];

as follows

[userAddedQuotes addObject:temp];
    [userAddedQuotes writeToFile:filepath atomically: NO];

The array gets updated in the runtime, but doesnt get saved to the plist.

+3  A: 

You cannot write to any file inside your application bundle (including your resource directory). If you want to write a file you should write it your apps Documents or Library folder, depending on what the file is. For example:

NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

Will return the an array of paths, the first of which is your documents directory.

Louis Gerbarg
Great. How do I write to the path? And I was wondering this before, I the file doesnt exist yet, how do I create a plist at that location? Regards
alJaree
You just write to it. Your above code is correct for writing to a file, the issue is you are trying to write to a file that is readonly. Point it to a writable location (such as the one I mentioned) and you should be fine.It is probably worth noting that as of iOS 4 and Snow Leopard it is preferred to use URL based methods (writeToURL:) and NSURLs, but it is not necessary.
Louis Gerbarg
Ok. I set it up correctly now, but when trying to write to it, the app crashes with no error. I think it because there isnt a file in the documents directory. How do I create on from within the code? Or is there a place the file can be put in the xcode project? regards
alJaree