保存用户名和高分的最佳方法是什么

发布于 2024-10-26 16:34:14 字数 82 浏览 6 评论 0原文

在我的应用程序中,我需要保存一个双值(高分)和字符串(玩家名称),我应该使用什么来获得这个。

任何想法都会很棒。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

握住我的手 2024-11-02 16:34:14

如果这就是您要保存的全部内容,则 NSUserDefaults 应该没问题

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

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 :)
兰花执着 2024-11-02 16:34:14

正如 deanWombourne 所说,您可以使用 NSUserDefaults 来存储这些数据,但它不是很安全。如果您不想“空中”存储这些数据,您可以查看 SFHFKeychainUtils by Buzz Andersen 将它们存储在 iPhone 钥匙串中。

首先,将 SFHFKeychainUtils 文件复制到您的项目中。单击 SFHFKeychainUtils.m,然后单击“获取信息”。转到目标选项卡并检查目标附近的框是否已选中。如果没有,请检查一下。按住 Control 键单击您的 Framework 文件夹,然后选择“添加现有框架”。找到 Security.framework 并将其添加到您的项目中。还可以通过执行与 SFHFKeychainUtils.m 相同的过程来检查该框架是否已添加到您的目标中。现在,在要使用此代码的位置打开您的实现文件,并在顶部添加 #import "SFHFKeychainUtils.h"

这是一个有关如何使用此代码的小示例:


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

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文