Writing to a property list on the iPod Touch not working but works in simulator
-
13-09-2019 - |
Question
In the application that I am writing I am trying to write a list of fortunes for a fortune cookie out to a property list. In the simulator this works as expected and everything is great. On the iPod Touch I have it reads from said list fine but it never updates the list.
Are there any differences between updating property lists using the simulator and using the iPod Touch?
if(indexEdit == [data count])
{
NSString *temp = [NSString stringWithFormat:@"%@", textField.text];
[self.data addObject:temp];
}
else
{
NSString *temp = [NSString stringWithFormat:@"%@", textField.text];
[data replaceObjectAtIndex:indexEdit withObject:temp];
}
NSString *errorDesc;
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Fortunes" ofType:@"plist"];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:data format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];
if (plistData)
{
[plistData writeToFile:bundlePath atomically:YES];
}
else
{
NSLog(errorDesc);
[errorDesc release];
}
Solution
I think that your application bundle is considered to be pretty much read only. If you want to write a file you should put it in the "Documents" folder, something like:
NSArray *savePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSMutableString *savePath = [NSMutableString stringWithString:[savePaths objectAtIndex:0]];
[savePath appendString:@"/Fortunes"];
// blah blah
[plistData writeToFile:savePath atomically:YES];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow