what is the best way for saving username and High Score

前端 未结 2 898
甜味超标
甜味超标 2021-01-15 17:26

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

相关标签:
2条回答
  • 2021-01-15 18:04

    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 :)
    
    0 讨论(0)
  • 2021-01-15 18:23

    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 讨论(0)
提交回复
热议问题