0
votes

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];
}
3
Did you set a breakpoint in applicationWillTerminate: to make sure the integer was stored? - Dylan Bettermann
What you mean by not working ? Is it always showing same value ? - Midhun MP
applicationWillTerminate is 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 using applicationDidEnterBackground or applicationWillResignActive to store your score. To confirm this, just set a breakpoint on the first line in applicationWillTerminate, simulate background/foreground and you'll see that your app will not be paused (breakpoint hit). - zrzka
"in Xcode"? are you sure it's "in Xcode" and not, rather, in your iOS app? - The Paramagnetic Croissant
Thank you all for your comments. As stated in the question, I am a beginner, so my ways are not too professional. When I say it is not working, I mean that when I re-open my app the value is reseted and yes, it is in my iOS app. I also don't know what 'setting a breakpoint' is, so I did not do so. Thank you again for all your comments! - CelceCode

3 Answers

1
votes

I would wrap the integer in an NSNumber object, and put this in applicationDidEnterBackground like so:

// Use this method to release shared resources, save user data, 
// invalidate timers, and store enough application state information to 
// restore your application to its current state in case it is terminated later.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
  highscoreInt = (int)[[NSUserDefaults standardUserDefaults] integerForKey:@"highscore"]; 

  [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highScoreInt] forKey:@"highscore"];
  [[NSUserDefaults standardUserDefaults] synchronize];

}

When you need to load your score, do the following inside whatever method you want to load it in:

int highScore = [[NSUserDefaults standardUserDefaults] objectForKey:@"highscore"];

You'll find this link helpful when reading up on NSUserDefaults.

1
votes

Use NSInteger instead of int

NSInteger highScore=0;

To store it

[[NSUserDefaults standardUserDefaults] setInteger:highScore forKey:@"HighScore"];

and to get it back

NSInteger highScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScore"];
0
votes

I strongly recommend saving the high score whenever it changes.