钥匙串未获取数据
我使用这里常见的钥匙串包装器: iOS:如何在应用程序中存储用户名/密码?
这是我的设置代码:
KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"Animex" accessGroup:nil];
[keychain setObject:username forKey:@"Username"];
[keychain setObject:password forKey:@"Password"];
[keychain release];
这是我的获取代码:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *password = [prefs stringForKey:@"Password"];
if ([password length] != 0)
{
return password;
}
else
{
return nil;
}
我只得到 NIL,我做错了什么?
I use the common Keychain-wrapper from here:
iOS: How to store username/password within an app?
Here is my Set-Code:
KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"Animex" accessGroup:nil];
[keychain setObject:username forKey:@"Username"];
[keychain setObject:password forKey:@"Password"];
[keychain release];
Here is my Get-Code:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *password = [prefs stringForKey:@"Password"];
if ([password length] != 0)
{
return password;
}
else
{
return nil;
}
I only get NIL back, what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用
KeychainItemWrapper
存储值,然后尝试使用NSUserDefaults
检索它们。这是两个完全不同的后备存储,因此您需要使用 KeychainItemWrapper 来检索您的值。您还应该使用kSecValueData
和kSecAttrAccount
。您链接到的文章实际上已经有答案。
You are storing the values using
KeychainItemWrapper
then trying to retrieve them usingNSUserDefaults
. Those are 2 completely different backing stores so you need to useKeychainItemWrapper
to retrieve your values. Also you should usekSecValueData
andkSecAttrAccount
.The article you linked to actually has the answer in there.