1

I have a strange problem with the KeyChainItemWrapper.

I used to create 2 of them like so:

KeychainItemWrapper *kcWrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"first" accessGroup:nil];

KeychainItemWrapper *kcWrapper1 = [[KeychainItemWrapper alloc] initWithIdentifier:@"second" accessGroup:nil];

I would then set an object like this:

[kcWrapper setObject:@"something" forKey:(__bridge id)kSecValueData];
[kcWrapper1 setObject:@"something1" forKey:(__bridge id)kSecValueData];

This worked perfectly, until... nothing changed?

Now I get this error:

*** Assertion failure in -[KeychainItemWrapper writeToKeychain],
/Users/wingair/Documents/iOSProjects/tulipstempelkort-

ios/stempelkort/../KeychainItemWrapper.m:305

Line 305 is:

// 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." );

Been stuck here for quite some long time, any help is appreciated.

The writeToKeyChain function:

- (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." );
WYS
  • 1,637
  • 2
  • 16
  • 37

0 Answers0