Ios 将其他变量添加到keychain中
我目前正在为我的iOS应用程序使用此版本的。但是,我很难添加其他变量 我希望能够将以下值添加到我的钥匙链中:Ios 将其他变量添加到keychain中,ios,security,access-token,keychain,Ios,Security,Access Token,Keychain,我目前正在为我的iOS应用程序使用此版本的。但是,我很难添加其他变量 我希望能够将以下值添加到我的钥匙链中: 用户名 密码 访问令牌 刷新令牌 目前我正在做这样的事情: KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"com.myname.myapp" accessGroup:nil]; [keychainItem setObject:usernameField.tex
- 用户名
- 密码
- 访问令牌
- 刷新令牌
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"com.myname.myapp" accessGroup:nil];
[keychainItem setObject:usernameField.text forKey:(__bridge id)(kSecAttrAccount)];
[keychainItem setObject:password1Field.text forKey:(__bridge id)(kSecValueData)];
- (NSString *) getPassword{
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"com.myname.app" accessGroup:nil];
NSString *password = [[NSString alloc] initWithData:[keychainItem objectForKey:(__bridge id)(kSecValueData)]
encoding:NSUTF8StringEncoding];
NSDictionary *jsonData =
[NSJSONSerialization JSONObjectWithData: [password dataUsingEncoding:NSUTF8StringEncoding]
options: NSJSONReadingMutableContainers
error: nil];
return jsonData[@"password"];
}
我尝试将多个对象设置为kSecValueData,但它只是覆盖了以前设置的对象
[keychainItem setObject:password1Field.text forKey:(__bridge id)(kSecValueData)];
[keychainItem setObject:access_token forKey:(__bridge id)(kSecValueData)];
[keychainItem setObject:access_token forKey:(__bridge id)(kSecValueData)];
所以我想知道是否有人知道我应该怎么做。。。我是否应该将我的
initWithIdentifier
更改为类似于@“com.myname.myapp.accesstoken”
?还有其他人有更好的方法吗?我不知道initWithIdentifier
或accessGroup
是如何工作的 所以我创建了一个变通方案,但我确信有更好的解决方案
我创建了一个将NSDictionary转换为JSON字符串的方法,并将JSON字符串插入到密钥链密码中,这样您就可以安全地存储多个值
NSDictionary *secretValues = [[NSDictionary alloc] initWithObjectsAndKeys:
password, @"password",
access_token, @"access_token",
refresh_token, @"refresh_token", nil];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:secretValues
options:0
error:&error];
if (! jsonData) {
NSLog(@"Got an error: %@", error);
} else {
NSString *jsonString = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
[keychainItem setObject:jsonString forKey:(__bridge id)(kSecValueData)];
}
然后,如果要获取密码,可以执行以下操作:
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"com.myname.myapp" accessGroup:nil];
[keychainItem setObject:usernameField.text forKey:(__bridge id)(kSecAttrAccount)];
[keychainItem setObject:password1Field.text forKey:(__bridge id)(kSecValueData)];
- (NSString *) getPassword{
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"com.myname.app" accessGroup:nil];
NSString *password = [[NSString alloc] initWithData:[keychainItem objectForKey:(__bridge id)(kSecValueData)]
encoding:NSUTF8StringEncoding];
NSDictionary *jsonData =
[NSJSONSerialization JSONObjectWithData: [password dataUsingEncoding:NSUTF8StringEncoding]
options: NSJSONReadingMutableContainers
error: nil];
return jsonData[@"password"];
}
我确信有更好的方法,但这是我能想到的最好的方法。+1很好的答案。这将存储行为类似于JSON对象的对象,例如字典或数组。。。这很好。我目前正在寻找最好的解决方案,如果找到这个帖子:。如果要维护对象的数据类型,应使用NSPropertyListSerialization。