2
votes

I've wrote a project using iOS keychain wrapper to store user name and password,
and the project works fine until Yesterday.
After I ran clean command to the project, the project crash in:

- (void)writeToKeychain
{
    NSDictionary *attributes = NULL;
    NSMutableDictionary *updateItem = NULL;

    // If the keychain item already exists, modify it:  
    if (SecItemCopyMatching((CFDictionaryRef)genericPasswordQuery,
                            (CFTypeRef *)&attributes) == noErr)
    {
        // First, get the attributes returned from the keychain and add them to the
        // dictionary that controls the update:
        updateItem = [NSMutableDictionary dictionaryWithDictionary:attributes];

    // Second, get the class value from the generic password query dictionary and
    // add it to the updateItem dictionary:
    [updateItem setObject:[genericPasswordQuery objectForKey:(id)kSecClass]
                   forKey:(id)kSecClass];

    // Finally, set up the dictionary that contains new values for the attributes:
    NSMutableDictionary *tempCheck = [self dictionaryToSecItemFormat:keychainData];

    //Remove the class--it's not a keychain attribute:
    [tempCheck removeObjectForKey:(id)kSecClass];

    // You can update only a single keychain item at a time.
    NSAssert(SecItemUpdate((CFDictionaryRef)updateItem,
                           (CFDictionaryRef)tempCheck) == noErr,
             @"Couldn't update the Keychain Item." );
}
else    
{
    // No previous item found; add the new item.
    // The new value was added to the keychainData dictionary in the mySetObject routine,
    //  and the other values were added to the keychainData dictionary previously.

    // No pointer to the newly-added items is needed, so pass NULL for the second parameter:
    //NSAssert(SecItemAdd((CFDictionaryRef)[self dictionaryToSecItemFormat:keychainData],
    //                  NULL) == noErr, @"Couldn't add the Keychain Item." );       
    NSLog(@"%ld", SecItemAdd((CFDictionaryRef)[self dictionaryToSecItemFormat:keychainData],NULL));
    NSLog(@"%ld", SecItemDelete((CFDictionaryRef)[self dictionaryToSecItemFormat:keychainData]));
}

}

In order to debug, I comment the NSAssert and add 3 NSLog.
However, I got error:

-25299(errSecDuplicateItem, The item already exists.)

of SecItemAdd and

-25300(errSecItemNotFound, The item cannot be found.)

of SecItemDelete

How do I remove the old keychain item in my device?

1
I have solved this issue according to this link:yhlin

1 Answers

0
votes

I remove old keychain item by SecItemDelete.

   NSMutableDictionary* yourKeychainDictionary;
   OSStatus status = SecItemDelete((CFDictionaryRef)yourKeychainDictionary);