KeyChainItemWrapper 有一个奇怪的问题。
我曾经像这样创建其中两个:
KeychainItemWrapper *kcWrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"first" accessGroup:nil];
KeychainItemWrapper *kcWrapper1 = [[KeychainItemWrapper alloc] initWithIdentifier:@"second" accessGroup:nil];
然后我会设置一个像这样的对象:
[kcWrapper setObject:@"something" forKey:(__bridge id)kSecValueData];
[kcWrapper1 setObject:@"something1" forKey:(__bridge id)kSecValueData];
这工作得很好,直到……什么都没有改变?
现在我得到这个错误:
*** Assertion failure in -[KeychainItemWrapper writeToKeychain],
/Users/wingair/Documents/iOSProjects/tulipstempelkort-
ios/stempelkort/../KeychainItemWrapper.m:305
第 305 行是:
// 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." );
被困在这里很长一段时间,任何帮助表示赞赏。
writeToKeyChain 函数:
- (void)writeToKeychain
{
CFDictionaryRef attributes = NULL;
NSMutableDictionary *updateItem = nil;
OSStatus result;
if (SecItemCopyMatching((__bridge CFDictionaryRef)genericPasswordQuery, (CFTypeRef *)&attributes) == noErr)
{
// First we need the attributes from the Keychain.
updateItem = [NSMutableDictionary dictionaryWithDictionary:(__bridge NSDictionary *)attributes];
// Second we need to add the appropriate search key/values.
[updateItem setObject:[genericPasswordQuery objectForKey:(__bridge id)kSecClass] forKey:(__bridge id)kSecClass];
// Lastly, we need to set up the updated attribute list being careful to remove the class.
NSMutableDictionary *tempCheck = [self dictionaryToSecItemFormat:keychainItemData];
[tempCheck removeObjectForKey:(__bridge id)kSecClass];
[tempCheck removeObjectForKey:(__bridge id)kSecAttrAccessGroup];
// An implicit assumption is that you can only update a single item at a time.
result = SecItemUpdate((__bridge CFDictionaryRef)updateItem, (__bridge CFDictionaryRef)tempCheck);
NSAssert( result == noErr, @"Couldn't update the Keychain Item." );