I have several user defaults set in my app:
Snap Shots ON/OFF Startup Tips ON/OFF Game Music ON/OFF iPod Music ON/OFF Sound Effects ON/OFF Reset All Dolls ON/OFF
When my app enters the backgound (in OS 4.0), my saveDefaults: method gets called from the applicationWillResignActive:(UIApplication *) method.
Then in the applicationWillEnterForeground: method, I load my User Defaults again. Then I call a method that starts the menuMusic if it is enabled.
Using fairly simple save default method that should work:
- (void)saveGameDefaults {
[[NSUserDefaults standardUserDefaults] setObject:(NSString *)myDoll.currentDoll forKey:@"DollName"];
[[NSUserDefaults standardUserDefaults] setInteger:[gameState intValue] forKey:@"GameState"];
[[NSUserDefaults standardUserDefaults] setInteger:[startScreen intValue] forKey:@"StartScreen"];
[[NSUserDefaults standardUserDefaults] setBool:snapShots forKey:@"SnapShots"];
[[NSUserDefaults standardUserDefaults] setBool:resetDolls forKey:@"Reset"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Here is a small portion of my load default method:
{
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"GameState"] == nil) gameState = [NSNumber numberWithInt:MENU_VIEW];
else gameState = [[NSUserDefaults standardUserDefaults] objectForKey:@"GameState"];
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"StartScreen"] == nil) startScreen = [NSNumber numberWithInt:SHOW_START_SCREEN];
else startScreen = [[NSUserDefaults standardUserDefaults] objectForKey:@"StartScreen"];
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"GameSound"] == nil) gameSound = [NSNumber numberWithInt:SOUNDS_ON];
else gameSound = [[NSUserDefaults standardUserDefaults] objectForKey:@"GameSound"];
}
It seems like, the defaults aren't updated or available to the app yet when it returns from the background. I am sending it to the background by switching directly to the iPhone settings, changing the settings, then switching directly back by double tapping the home button and choosing my app. But if I go back out again and come back in it WILL read the new Settings that were set.
For example: 1. I have music enabled and I start the app. The music is playing at the menu. 2. I double tap the home button and choose my Settings app & change the Menu Music setting to NO. 3. Double Tap the home button and change back to game. Music is still playing. 4. Double tap the home again and go back to Settings App. Menu Music is still set to NO. 5. Switch back to my app a second time and now the music is not playing any more.
Any one else having a problem with updating User Settings while app is in background??
Thanks in advance - Mark