保存在KeyChainItemWrapper崩溃的密码
Apple在其GenericKeyChain示例代码中提供了KeyChainItemWrapper类。 在这里有一个ARC'ed解决scheme,我试图遵循: 封装存储在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)];
带有电子邮件文本字段的行被接受。 但是密码的第二行崩溃,出现以下exception。
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是一个lib方法。 正如我所期待的,这与KeyChain处理直接有关…
keychainItemData包含:
我无法得到这个苹果的例子Keychain包装工作了。 幸运的是,对这个问题的进一步研究揭示了这个解决scheme ,它为我工作。
当心解决scheme的原始答案不是ARC'ed,但有人在Github上创build一个ARC'ed版本 。 我用那个,像魅力一样工作。
这是钥匙圈的包装,比原来更简单。
希望这可以帮助有类似问题的其他人。
在模拟器上运行应用程序时,我有同样的错误,但它在设备上运行完美。
为了解决与模拟器的问题,我不得不打开“共享钥匙串权利”。
共享钥匙串权利