Question

I want to delete an account from my SSKeychain. I only get the method to delete the password, but the account is still in the keychain.

Is there a way to delete an account or clear the SSKeychain?

Was it helpful?

Solution

What I'd use to delete an account,:
1. Instance a SSKeychainQuery object.
2. set the service and the account, that I want to delete.
3. call the method deleteItem:

//1  
SSKeychainQuery *query = [[SSKeychainQuery alloc] init];  
//2  
query.service = @"MyService";  
query.account = @"account2";  
//3  
[query deleteItem:nil];

Then if you call

NSLog(@"%@",[SSKeychain accountsForService:@"MyService"]);

This will print all the accounts for the service called "MyService" or null if there's not "MyService"

OTHER TIPS

Easiest way to clear the Keychain is like this:

SSKeychainQuery *query = [[SSKeychainQuery alloc] init];

NSArray *accounts = [query fetchAll:nil];

for (id account in accounts) {

    SSKeychainQuery *query = [[SSKeychainQuery alloc] init];

    query.service = @"<INSERT SERVICE NAME>";
    query.account = [account valueForKey:@"acct"];

    [query deleteItem:nil];

}

How about

+ (BOOL)deletePasswordForService:(NSString *)service account:(NSString *)account;

Or to delete all accounts:

NSArray *accounts = [SSKeychain allAccounts];
for (int i = 0; i < accounts.count ; i++) {
    [SSKeychain deletePasswordForService:service account:[accounts objectAtIndex:i]];
}

From the looks of the code it doesn't seem possible to change an account at all (except adding passwords for services to it).

Nobody seems to be interested in that feature judging from the discussions on Github.

My advice: Go here and post a new Issue about this. I think this should be doable, at least they can tell you for certain if there already is a way with the current version of SSKeyChain.

EDIT

On second thought, wouldn't deleting all password for a certain account make the account "non-existent"?

in SSKeychainQuery I found the method to delete an account

/**
 Dete keychain items that match the given account, service, and access group.

 @param error Populated should an error occur.

 @return `YES` if saving was successful, `NO` otherwise.
 */
- (BOOL)deleteItem:(NSError **)error;

But this is the method, which deletPassword uses. So actually deleteItem only deletes the password not the whole item

+ (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account error:(NSError *__autoreleasing *)error {
    SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
    query.service = serviceName;
    query.account = account;
    return [query deleteItem:error];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top