1

In my app I need to save a double value (high score) and String (player name) what should i use to get this.

any idea will be great.

Thank you

Bobj-C
  • 5,276
  • 9
  • 47
  • 83

2 Answers2

3

If this is all you're saving then NSUserDefaults should be fine

// To store
[[NSUserDefaults standardUserDefaults] setObject:name forKey:@"name"];
[[NSUserDefaults standardUserDefaults] setDouble:score forKey:@"score"];

// To read back in
NSString *name = [[NSUserDefaults standardUserDefualts] objectForKey:@"name"];
double score = [[NSUserDefaults standardUserDefaults] doubleForKey:@"score"];
// Don't forget that your name is autoreleased - if you want to keep it, set it to a retained
// property or retain it yourself :)
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • hello thanks for answering but in this way if the user have a jailbroken iphone he can access com.domainname.app.plist and change the his score to whatever value he want. can i hide the key value inside .plist – Bobj-C Mar 27 '11 at 10:54
  • Ah, you never said secure :) This way is easiest but if you want security use @Matthew's answer instead :) – deanWombourne Mar 27 '11 at 11:23
  • The keychain isn't going to secure the data from the user. While it is definitely more work to access the keychain data than the preferences plist, it's only meant to protect the user's data from others (i.e. people who don't know the user's passcode). You might as well use NSUserDefaults and encrypt the values using a key stored in the application code. – Daniel Dickison Apr 06 '11 at 15:30
  • Oh, I thought that library would do encryption as well. Oh well, every day's a school day! – deanWombourne Apr 07 '11 at 09:26
1

As deanWombourne said, you can use NSUserDefaults to store these data but it isn't very secure. If you don't want to store this data "in the air", you can take a look to SFHFKeychainUtils by Buzz Andersen to store them in the iPhone Keychain.

First of all, copy SFHFKeychainUtils files to your project. Click on the SFHFKeychainUtils.m and click on Get Info. Go to Target tab and check if the box near your target is checked. If not, check it. Control-click on your Framework folder and select Add Existing Framework. Find Security.framework and add it to your project. Check also that this framework is added to your target by doing the same procedure done for SFHFKeychainUtils.m. Now open you implementation file on where you want to use this code and add on the top #import "SFHFKeychainUtils.h".

This is a little example on how to use this code:


// to store your data  
NSError *error = nil;  
[SFHFKeychainUtils storeUsername:kName andPassword:name forServiceName:kStoredName updateExisting:YES error:&error];  
[SFHFKeychainUtils storeUsername:kScore andPassword:score forServiceName:kStoredScore updateExisting:YES error:&error];  

// to get them back  
NSString *name = [SFHFKeychainUtils getPasswordForUsername:kName andServiceName:kScoredName error:&error];  
double score = [SFHFKeychainUtils getPasswordForUsername:kScore andServiceName:kScoredScore error:&error];  

// kName, kScore, kStoredName, kStoredScore are defined key but you can use also strings with @"your string here".
// It is important that when you store and get back a value, username and serviceName must be the same.
matteodv
  • 3,992
  • 5
  • 39
  • 73
  • the problem is when i'am assigning score (double value) an error occurred incomparable type – Bobj-C Mar 27 '11 at 11:45
  • try to convert double to a string before store the score and when you want to get it back convert it again to a double... – matteodv Mar 27 '11 at 11:47
  • igot those errors many times +[SFHFKeychainUtils storeUsername:andPassword:forServiceName:updateExisting:error:] in SFHFKeychainUtils.o _kSecClassGenericPassword$non_lazy_ptr in SFHFKeychainUtils.o – Bobj-C Mar 27 '11 at 12:10
  • No, not a string! Convert it to an NSNumber object - `[NSNumber numberWithDouble:score]` and get the value back by using doubleValue :) – deanWombourne Mar 27 '11 at 12:11
  • @Bobj-C Have you imported the file on the top with #import "SFHFKeychainUtils.h" ? Have you added SFHFKeychainUtils.h and .m in your project? – matteodv Mar 27 '11 at 12:19
  • And add Security.framework to your project, clean and try to rebuild... Sorry, I forgot to tell you :) – matteodv Mar 27 '11 at 12:22
  • edited the answers with a detailed description... before build your app, clean all targets and rebuild... – matteodv Mar 27 '11 at 12:39