0

Is it possible to save a high score for an iPhone app after the app is turned off (pressing the home button and returning to the main screen)? I'm not using an online high score system or anything like that, I just want it so that the user can see the high score from the last time they played the game when they pull the app up again.

0

3 Answers 3

2

A really quick and dirty way of doing this might be using NSUserDefaults, but if it's a game you might want to look into a longer term solution using GameCenter.

2
  • Thanks for the suggestion, but this is actually a really simple game, so I don't need to use anything like GameCenter
    – RaysonK
    Mar 7, 2012 at 17:38
  • Agreed, for simple storage use NSUserDefaults. Mar 7, 2012 at 17:39
1

In short,

//To save the score (in this case, 10000 ) to standard defaults:

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


// To read it back:

int highscore = [[NSUserDefaults standardUserDefaults] integerForKey:@"highscore"] ;

As others have suggested, you might check out the NSUserDefaults class reference to see how/when to store items in the user defaults.

3
  • would this work for primitives, or do I have to use an object?
    – RaysonK
    Mar 7, 2012 at 17:58
  • Sorry, was assuming a string for some reason. Edited for integer.
    – Mike Fahy
    Mar 7, 2012 at 18:07
  • Ah, that would be a good method! Thanks
    – RaysonK
    Mar 7, 2012 at 18:23
0

You have to use iPhone storage to keep local high scores which is well explained there :
How to use iPhone Storage

Not the answer you're looking for? Browse other questions tagged or ask your own question.