I have a fairly simple iPhone application that I am making. However, I cannot get my NSUserDefaults to save permanently. Writing and retrieving the data is not a problem––I can save the data (in my case, a string), and retrieve it on command, even after switching views, closing/opening the application, etc, and the data is retrieved just fine. It appears as if the string was saved properly to the key. But when I quit the application from the multitasking tray and restart it, the settings are no longer saved, and the application boots up like it is the first time. I am a novice programmer, so it is probably just a stupid error on my part.
Here's what the saving looks like:
if (optionsSoundBox.center.x >= 240)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.2];
self.soundOnLabel.alpha = 1;
self.soundOffLabel.alpha = 0;
[UIView commitAnimations];
NSUserDefaults *soundOptions = [NSUserDefaults standardUserDefaults];
[soundOptions setObject:@"SoundsON" forKey:@"SoundKey"];
[soundOptions synchronize];
}
if (optionsSoundBox.center.x < 240)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.2];
self.soundOnLabel.alpha = 0;
self.soundOffLabel.alpha = 1;
[UIView commitAnimations];
NSUserDefaults *soundOptions = [NSUserDefaults standardUserDefaults];
[soundOptions setObject:@"SoundsOFF" forKey:@"SoundKey"];
[soundOptions synchronize];
}
I retrieve the string in the viewDidLoad so it will be ready on startup like this:
NSUserDefaults *soundOptions = [NSUserDefaults standardUserDefaults];
NSString *savedSoundSettings = [soundOptions stringForKey:@"SoundKey"];
if (savedSoundSettings == @"SoundsON")
{
[optionsSoundBox setCenter:CGPointMake(280, optionsSoundBox.center.y)];
}
if (savedSoundSettings == @"SoundsOFF")
{
[optionsSoundBox setCenter:CGPointMake(200, optionsSoundBox.center.y)];
}
I really appreciate any help you can give