ios - 在 KeyChainItemWrapper 中保存密码时崩溃

标签 ios ios7 keychain keychainitemwrapper

Apple 在其 GenericKeyChain sample code 中提供了 KeyChainItemWrapper 类. SO 上有一个 ARC'ed 解决方案,我正在尝试遵循:wrapper存储在 iOS 上的 KeyChain 中。

包装器的用法是这样的:

KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"F11-email-auth" accessGroup:nil];
[keychain setObject:[emailTextfield text] forKey:(__bridge id)(kSecMatchEmailAddressIfPresent)];
[keychain setObject:[passwordTextfield text] forKey:(__bridge id)(kSecClassGenericPassword)];

接受带有电子邮件文本字段的行。 但是带有密码的第二行崩溃并出现以下异常。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn't add the Keychain Item.'
*** First throw call stack:
(
    0   CoreFoundation                      0x01b445e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x018c78b6 objc_exception_throw + 44
    2   CoreFoundation                      0x01b44448 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x014a823e -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   Feeltracker                         0x000053b3 -[KeychainItemWrapper writeToKeychain] + 899
    5   Feeltracker                         0x00004700 -[KeychainItemWrapper setObject:forKey:] + 272
    6   Feeltracker                         0x000092d6 -[FTLoginViewController connectToAccount:] + 374
    7   libobjc.A.dylib                     0x018d9874 -

可能是什么原因?我想知道它是否与我使用的常量有关。

更新:

感谢rmaddy的帮助:

这是似乎引发错误的位:

// No previous item found; add the new one.
result = SecItemAdd((__bridge CFDictionaryRef)[self dictionaryToSecItemFormat:keychainItemData], NULL);
NSAssert( result == noErr, @"Couldn't add the Keychain Item." );

结果为 -50。 SecItemAdd 是一个库方法。正如我所料,这在某种程度上与 KeyChain 直接处理有关...

keychainItemData 包含: enter image description here

最佳答案

我再也无法使用这个 Apple 钥匙串(keychain)包装器示例了。 幸运的是,对此事的进一步研究揭示了这一点 solution ,这对我有用。

请注意,解决方案的原始答案并未经过 ARC 处理,但是有人非常友好地创建了一个 ARC'ed version on Github .我用了那个,效果很好。

它是 key 链的包装器,比原来的更简单。

希望这能帮助其他有类似问题的人。

关于ios - 在 KeyChainItemWrapper 中保存密码时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19749160/

相关文章:

ios - Background Fetch iOS - 已终止的应用程序

ios - 定时时钟动画

objective-c - SecKeychainFindInternetPassword 不工作

android - 在 Android 上是否有与 iOS 的钥匙串(keychain)等效的用户凭据?

ios - 键盘隐藏通知调用多次

ios - iOS 中不同尺寸 View 的约束 "equal heights"?

ios - 无法弄清楚为什么我收到错误 "PFPush does not have a member named sendPushInBackground"

ios - "Ambiguous reference to member ' init(...)"调用基类初始化器

ios - 将 ios 6 应用程序转换为 ios 7

xcode - iOS 分发 : Migrating private key/certs to new machine