I an a beginner wanting to to store an int value (high score) in my app so when the user closes the app from the multitasking and opens it again it will get the data and display it.
I tried using NSUserDefaults, but it does not appear when I open the app again. Here is the code I used:
// Used this to load the high score from the memory
highscoreInt = (int)[[NSUserDefaults standardUserDefaults] integerForKey:@"highscore"];
// And this to store it, later in the code
- (void)applicationWillTerminate:(UIApplication *)application
{
[[NSUserDefaults standardUserDefaults] setInteger:highscoreInt forKey:@"highscore"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
applicationWillTerminate:to make sure the integer was stored? - Dylan BettermannapplicationWillTerminateis called when the app is going to be purged from memory. In other words, if you put your app to the background, then back to the foreground, this method is not called. Consider usingapplicationDidEnterBackgroundorapplicationWillResignActiveto store your score. To confirm this, just set a breakpoint on the first line inapplicationWillTerminate, simulate background/foreground and you'll see that your app will not be paused (breakpoint hit). - zrzka