开发者

what is the best way for saving username and High Score

In my app开发者_StackOverflow社区 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


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 :)


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜