I have some settings in a plist, but when I kill my app I lose all the data stored there.
This is the code that I’m using:
.h
@property (retain, nonatomic) NSString *plistFilePath;
-(IBAction)setHomepage:(id)sender;
.m
@syntehzise plistFilePath;
-(IBAction)setHomepage:(id)sender{
plistFilePath = [NSString stringWithString:[[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"]];
NSMutableDictionary *data= [[NSMutableDictionary alloc] initWithContentsOfFile:plistFilePath];
[data setObject:@"http://www.google.com" forKey:@"Homepage"];
[data writeToFile:plistFilePath atomically:YES];
[data release];
}
Am I doing something wrong? Should I use a different class or differents methods? Please help me because I don’t know why I store well the information but then when I kill the app I lose it.
As already mentioned the bundle is read only.
Try to avoid setting ‘settings’ in an a copied plist, as plists are just one more thing to managed. Instead, why not use NSUserDefaults and import your defaults from a defaults plist. For example, add a new plist to your project, and add this to your delegate:
Now you can save data like this:
And retrieve it like this:
And you don’t have to worry about external files. Naturally your defaults plist can be platform, user or device specific!
Hope this helps!