浏览器家园·资讯

展开

钥匙串

编辑:浏览器知识

钥匙串

框架地址

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];
}

转自:https://blog.csdn.net/feiyangkl/article/details/53421887

文章TAG:钥匙  钥匙串  

加载全部内容

相关教程
猜你喜欢
大家都在看