Thanks for helping, I found what happend, it turns out an outside library is using our app bundle identifier created a new keychain.
So every time I do a search it serched for the wrong thing.
Right now I am doing a migration on the keychainManager, I am trying looping through the keychain items and delete the old one I added.
Here is my code
NSMutableDictionary *searchKeychainItem = [NSMutableDictionary dictionary];
searchKeychainItem[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;
searchKeychainItem[(__bridge id)kSecAttrAccessible] = (__bridge id)kSecAttrAccessibleAfterFirstUnlock;
searchKeychainItem[(__bridge id)kSecAttrService] = BlinkServiceKey;
searchKeychainItem[(__bridge id)kSecReturnData] = (__bridge id)kCFBooleanTrue;
searchKeychainItem[(__bridge id)kSecReturnAttributes] = (__bridge id)kCFBooleanTrue;
searchKeychainItem[(__bridge id)kSecMatchLimit] = (__bridge id)kSecMatchLimitAll;
searchKeychainItem[(__bridge id)kSecReturnPersistentRef] = (__bridge id)kCFBooleanTrue;
CFArrayRef result = nil;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)searchKeychainItem, (CFTypeRef *)&result);
if (status == errSecSuccess && result != nil){
NSArray *resultArray = (__bridge_transfer NSArray *)result;
for (NSDictionary *keychainItem in resultArray){
NSString *userEmail = keychainItem[(__bridge id)kSecAttrAccount];
//check for if the account is a email address format
if(IsEmailAddressValidLooking(userEmail)){
//delete old email/password keychain item
NSDictionary *deleteItem = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)(kSecClassIdentity), kSecClass,
[NSArray arrayWithObject:keychainItem], kSecMatchItemList,
kSecMatchLimitOne, kSecMatchLimit,
nil];
OSStatus deleteStatus = SecItemDelete((__bridge CFDictionaryRef)deleteItem);
if(deleteStatus != errSecSuccess){
//failed here with error code -50
@throw [NSException exceptionWithName:BlinkKeychainManagerException reason:[NSString stringWithFormat:@"Failed to delete old email/password keychain item with error code %d", deleteStatus] userInfo:nil];
}
break;
}
}
}
It failed on the delete, here is the apple document link for this https://developer.apple.com/documentation/security/ksecmatchitemlist?language=objc
"
To delete an item identified by a persistent reference, specify the
kSecMatchItemList
search key in a call to the
SecItemDelete
function with a persistent reference returned by using the
kSecReturnPersistentRef
return type key to the
SecItemCopyMatching
or
SecItemAdd
functions.
"
How do I use persistent reference to delete a keychain item that returned by SecItemCopyMatching?
Am I doing it wrong?