[英]iPhone: Save user data in plist, SQLite or cookie?
My app (like most) is going to be leveraging many remote services... so when a user authenticates themselves, I need to store their username and password (or some kind of flag) so that they don't have to authenticate all over the app. 我的应用程序(像大多数人一样)将利用许多远程服务......所以当用户验证自己时,我需要存储他们的用户名和密码(或某种标志),这样他们就不需要对所有人进行身份验证该应用程序。
Where is the best/quickest/easiest place to store this user data? 存储此用户数据的最佳/最快/最简单的位置在哪里?
You can still store the username and server URL with NSUserDefaults, but Keychain services is the best idea if you're storing a password. 您仍然可以使用NSUserDefaults存储用户名和服务器URL,但如果您要存储密码,则最好使用Keychain服务。 It's part of the C-Based security framework, and there'a a great wrapper class SFHFKeychainUtils , to give it an Objective-C API. 它是基于C的安全框架的一部分,并且有一个很好的包装类SFHFKeychainUtils ,为它提供一个Objective-C API。
To save: 要保存:
NSString *username = @"myname";
NSString *password = @"mypassword";
NSURL *serverURL = [NSURL URLWithString:@"http://www.google.com"];
[SFHFKeychainUtils storeUsername:username andPassword:password forServiceName:[serverURL absoluteString] updateExisting:YES error:&error]
To restore: 恢复:
NSString *passwordFromKeychain = [SFHFKeychainUtils getPasswordForUsername:username andServiceName:[serverURL absoluteString] error:&error];
NSUserDefaults NSUserDefaults的
Save like this: 像这样保存:
[[NSUserDefaults standardUserDefaults] setObject:username forKey:@"username"];
[[NSUserDefaults standardUserDefaults] setObject:password forKey:@"password"];
[[NSUserDefaults standardUserDefaults] synchronize];
Fetch like this: 像这样取:
NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:@"username"];
NSString *password = [[NSUserDefaults standardUserDefaults] stringForKey:@"password"];
Storing secure data in an insecure file (user defaults) isn't usually a good idea. 将安全数据存储在不安全的文件中(用户默认值)通常不是一个好主意。 Look into Keychain Services, which encrypts sensitive login information and such. 查看Keychain Services,它可以加密敏感的登录信息等。
http://developer.apple.com/library/mac/#documentation/Security/Conceptual/keychainServConcepts/iPhoneTasks/iPhoneTasks.html http://developer.apple.com/library/mac/#documentation/Security/Conceptual/keychainServConcepts/iPhoneTasks/iPhoneTasks.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.