0
votes

I have been learning about using the Keychain to store username and password combinations. I have the following method called "clear Password" but it seems to delete the entire keychain item for the account name:

-(OSStatus)clearPassword{

 NSDictionary *query =  @{ (__bridge  id)kSecClass :  (__bridge  id)kSecClassGenericPassword,
                          (__bridge  id)kSecAttrAccount : self.accountName};


CFDictionaryRef queryRef = (__bridge CFDictionaryRef)query;
OSStatus status =  SecItemDelete(queryRef);
NSAssert(status == errSecSuccess || errSecItemNotFound, @"Unable to delete keychain item");

return status;


}

is it possible to delete JUST the password for a specific account Name so that I would keep the account name in the keychain with no password so that when the user goes to log in the app I can check if the accountname they entered is in the keychain already? If so how would I formulate the query Dictionary to accomplish this?

1
The keychain is intended for storing passwords. An entry without anything to protect does not belong in the keychain to put it simply. Can't you store the last used username somewhere else (NSUserDefaults, for example?).Krumelur

1 Answers

2
votes

I agree with Krumelur's comment. When I've done this in the past, I've stored the last signed on username in the NSUserDefaults. If that username has an entry in the keychain, the user is signed in automatically. If not, the user is prompted to sign in and their credentials are stored in the keychain until they sign back out.