标签:
钥匙串
框架地址
https://github.com/soffes/sskeychain
/// 所有账户
+ (NSArray *)allAccounts;
/// 获取所有账户信息
+ (NSArray *)accountsForService:(NSString *)serviceName;
/// 获取账号密码
+ (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account;
/// 删除账号密码
+ (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account;
/// 将账号密码保存在钥匙串
+ (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account;
保存钥匙串
/// 保存用户信息
- (void)saveUserInfo {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// iOS 8.0 会直接写入沙盒,无需再使用 `[defaults synchronize];`
[defaults setObject:self.username forKey:HMUsernameKey];
// 将用户密码保存在钥匙串
NSString *bundleId = [NSBundle mainBundle].bundleIdentifier;
[SSKeychain setPassword:self.pwd forService:bundleId account:self.username];
}
从钥匙串加载密码
/// 加载用户下信息
- (void)loadUserInfo {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.usernameText.text = [defaults stringForKey:HMUsernameKey];
NSLog(@"%@", [SSKeychain allAccounts]);
NSString *bundleId = [NSBundle mainBundle].bundleIdentifier;
self.pwdText.text = [SSKeychain passwordForService:bundleId account:self.usernameText.text];
}
标签:
原文地址:http://www.cnblogs.com/fakeCoder/p/5093785.html