Question

I have a Root.plist file which is used for my app's settings. It has a toggle switch with an identifier of reset_achievements_preference. In the applicationDidBecomeActive method, I have this code:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSLog(@"reset achievements: %i", [[NSUserDefaults standardUserDefaults] boolForKey:@"reset_achievements_preference"]);

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"reset_achievements_preference"]) {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"reset_achievements_preference"];
        [[NSUserDefaults standardUserDefaults] synchronize];

        //Code to react to this change
    }
}

Sometimes it hits the NSLog and notices the object change, but sometimes it doesn't. I wonder if I'm dealing with this incorrectly?

Was it helpful?

Solution

Try adding:

[[NSUserDefaults standardUserDefaults] synchronize]

To applicationDidBecomeActive: before anything else to refresh the status of the user defaults. Thy synchronize method is called periodically by the application, but you can refresh it manually.

OTHER TIPS

When do you want to handle your reset_achievements_preference option? The method applicationDidBecomeActive is called when the application starts and when the application returns from background (is brought to foreground by the user).

If you want to handle reset_achievements_preference only at application startup, it is possible that the user puts your app into background, then returns to it. In this case applicationDidBecomeActive is called and it sets reset_achievements_preference to NO which probably is not what you want.

You can simply move this code to application:didFinishLaunchingWithOptions: method to solve this problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top