Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm creating a game using Cocos2D (objective-c) and it has come to the point where I have to save data to local storage, like high scores and similar stuff.

I know that NSUserDefaults is not a good option because it isn't encrypted. I would like to know what is or what are the best/simple methods of saving data with security to the iPhone to avoid a casual hacker from changing the score.

share|improve this question
Fundamentally this is the same question as… There shouldn't be anything specific to the iPhone that would make the answer any different, unless there's some kind of built-in mechanism in the SDK for doing so. – Tetrad Apr 4 '12 at 23:23
There is such a mechanism built into iOS (the Keychain), but it's really intended for passwords and probably isn't a good fit for saved games. – John Calsbeek Apr 5 '12 at 4:56
@JohnCalsbeek, So better not to use keychain? – marcg11 Apr 5 '12 at 10:55
@marcg11 Yeah, it's not intended for chunks of secure content, just for passwords. – John Calsbeek Apr 5 '12 at 16:26
up vote 1 down vote accepted

Personally, I wouldn't try to encrypt the file. It makes writing and parsing more complicated (eg. you'll have to work around the built-in mechanisms) and it's also much harder to debug, because you can't have a look what your app just wrote there.

Instead just write to the application directory or use NSUserDefaults.

Now this doesn't address your issue about hacking of these files. To add some protection I'd suggest that you create a hash (checksum) of that data and store it in the Keychain. Then every time you read the data back in, you compare the stored hash to the one that you'll get from the read data. If it's the same, everything is fine. If the data has been altered, your checksums won't match and you can do any counter-measure you'd like...

share|improve this answer
How exactly this cheksum works? I know with NSUserDefaults I can set the score by doing the following setInteger:score forKey:@"myScore"]; When you say store the cheksum in the keychain , whatt do you mean? – marcg11 Apr 5 '12 at 10:48
By checksum/hash, I mean something like MD5. Then you generate a hash of your data (eg. everything that gets written to the file). If the data changes, it won't generate the same hash... – bummzack Apr 5 '12 at 12:58
Maybe this article will help you. – bummzack Apr 5 '12 at 13:00
Nice article. I won't save a lot of things, so it's better to send a string instead of NSData, ,right? That string is just all the things i want to save and more (like the udid, etc) concatenated? – marcg11 Apr 5 '12 at 13:39
I'd use an NSDictionary as container for your settings. You can easily write that to a .plist file or serialize it to NSData. That's going to be flexible, even when you plan to write more contents later on. – bummzack Apr 5 '12 at 14:00

Well if you're worried about secure data, I would encrypt it yourself regardless of where it's stored. Since I don't do iOS programming I don't really know where you can store it but I would suggest also storing it online if possible. That way, you always have control over the data. But, if that's not an option, (if it lets you) store it in the app folder encrypted. Hope this helps.

share|improve this answer

If you want to store the players score than NSUserDefaults is the best option to store it as no one can change it except your application itself.

There are other options available in iOS devices, like in a file in the documents folder of your application in the form of plist, sqlite3 or normal text file, which can be modified by iExplorer application for mac and windows. But you don't have to worry about that as most of the iPhone users won't go to that much level to just change their score. They are gonna play ur game instead, if they like it.

So you don't need to worry about the where to store your application data. But even if you are worried than there are some encryption classes available in iOS framework that you can use to store your data.

share|improve this answer
The thing is that I'm new to iOs programming, and I can't find a good source of code of encryption, or the ones I find aren't very clear for me. – marcg11 Apr 5 '12 at 10:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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