1

I have this game where the user collects coins and I want them to be able to save them and use the coins for in app purchaces. How would I do this? Could I use NSUserDefaults for this or is it something else? Thanks! I used this for saving the highScore:

                   EDITED
    var coinDefaults = NSUserDefaults()
    var coinScore = defaults.integerForKey("coinScore")

    if(coins > 0)
    {
        coinDefaults.setInteger(coins, forKey: "coinScore")
    }
    var showCoins = coinDefaults.integerForKey("coinScore")

    coinLabel.text = String(showCoins)



    }
swifter22
  • 65
  • 9

2 Answers2

3

Reseting the coinScore

NSUserDefaults.standardUserDefaults().removeObjectForKey("coinScore")

loading the saved score

var coinScore =  NSUserDefaults.standardUserDefaults().integerForKey("coinScore")

var coins = 5

updating the coinScore

if coins > coinScore  {
    coinScore = coins
    NSUserDefaults().setInteger(coinScore, forKey: "coinScore")
}

println( NSUserDefaults().integerForKey("coinScore").description )

coins += 20

if coins > coinScore  {
    coinScore = coins
    NSUserDefaults().setInteger(coinScore, forKey: "coinScore")
}

println( NSUserDefaults().integerForKey("coinScore").description )
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
1

yes, you have different options to save data : nsuserdefaults, write to a file and store the file, coredata, or use a webserver with a database.

If someone knows how to hack your app, he will use what is inside your app (the 3 first methods), this is why many big games use internet (webserver) to check against what was bought by the user.

But, it is not so often that someone hack your game, if he does though, he would never pay anyway. So stay with nsuserdefaults, it is simple and good enough in my opinion.

Paul
  • 6,108
  • 14
  • 72
  • 128
  • I have this code but its not saving the coins. I put it in the op. – swifter22 Mar 05 '15 at 15:36
  • what is the coins += 20 for? – swifter22 Mar 05 '15 at 16:51
  • @swifter22 it is the same as `coins = coins + 20` – Paul Mar 05 '15 at 16:53
  • I dont get why its adding 20 to the coins....isn't it supposed to start at 0 and go up everytime the user collects a coin. – swifter22 Mar 05 '15 at 16:54
  • @swifter22 you put the values you want........ you retrieve the value from the nsuserdefaults, then you add the value you want to the variable, and you put the result back to nsuserdefaults. – Paul Mar 05 '15 at 16:58
  • Thanks for the help I understand how to do it now. – swifter22 Mar 05 '15 at 16:59
  • I agree with Paul about security, and you should keep in mind that .plist are unsafe and human readable...You can make things little harder to hack, but without server or some kind of encryption you are always vulnerable in some percentage. Also cheaters/hackers in the first place probably would never pay for anything, anyways. Here one of my questions about security stuff: http://stackoverflow.com/questions/27566483/storing-game-preferences-and-saved-games-in-a-secure-format – Whirlwind Mar 05 '15 at 17:36