Служба связки ключей для iOS, ошибка удаления цепочки для ключей - PullRequest
2 голосов
/ 28 апреля 2011

Я написал проект с использованием Оболочки для ключей iOS для хранения имени пользователя и пароля,
, и проект работает до вчерашнего дня. После того, как я запустил чистую команду для проекта, сбой проекта:

- (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]));
}

}

Для отладки я комментирую NSAssert и добавляю 3 NSLog.
Однако я получил ошибку:

-25299 (errSecDuplicateItem, элемент уже существует.)

из SecItemAdd и

-25300 (errSecItemNotFound, Элемент не может быть найден.)

из SecItemDelete

Как удалить старый элемент цепочки для ключей на моем устройстве?

1 Ответ

0 голосов
/ 10 августа 2018

Я удаляю старый элемент цепочки для ключей с помощью SecItemDelete.

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