47

I'm testing login flow (using KeychainItemWrapper) inside my app on a device. How do I reset/delete keychain for my app?

On the Simulator, I do it by clicking on iOS Simulator -> Reset Content and Settings....

syntagma
  • 23,346
  • 16
  • 78
  • 134
  • Possible duplicate of http://stackoverflow.com/questions/7142774/reset-an-iphone-apps-keychain – Daniel Martín May 02 '13 at 11:27
  • 2
    @DanielMartín The question you're linking to is about resetting keychain programmatically (which I do inside my app). I want to know how to do it manually for the testing purposes. – syntagma May 02 '13 at 11:29
  • 1
    The only way I know to accomplish that is from the device Settings, General, Reset, Reset All Settings. It will reset the keychain for every app installed on the device. – Daniel Martín May 02 '13 at 11:41
  • 6
    I upvoted that but realized the answer is incorrect. It has not deleted the keychain of my app even after uninstalling the app and then "Reset All Settings". – Jonny Aug 02 '13 at 09:26
  • 1
    @Jonny Maybe iCloud keychain or something? Not sure how you are storing your data in the Keychain. I would create a "logout" method inside my app to delete all data manually. – Maciej Swic May 07 '14 at 13:44
  • Possible duplicate of [How to delete all keychain items accessible to an app?](http://stackoverflow.com/questions/14086085/how-to-delete-all-keychain-items-accessible-to-an-app) – serge-k Mar 02 '16 at 17:03

4 Answers4

14

Keychain items are in iOS sandbox, users don't have access to remove unwanted keychain item. These are accessible via API's only.

KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:[[NSBundle mainBundle] bundleIdentifier] accessGroup:nil]; 

//or how you access your keychain

[keychainItem resetKeychainItem];

or you can reset your device >> from the device Settings, General, Reset, Reset All Settings. But, it will reset the keychain for every app installed on the device.

syntagma
  • 23,346
  • 16
  • 78
  • 134
Will
  • 152
  • 1
  • 9
1

you can dump keychain data using Keychain dumper. Grab the following link https://github.com/ptoomey3/Keychain-Dumper

Just go to this url and download the zip file and unzip it. Inside this folder, the only file that we are interested is the keychain_dumper binary. The information that is allowed to be accessed by an application in the keychain is specified in its entitlements. This binary is signed with a self signed certificate with wildcard entitlements and hence it is able to access all the keychain items. There could also have been other ways to make sure all the keychain information is granted, like having the entitlements file contain all the keychain access groups or using a specific keychain access group that provides access to all the keychain data. For e.g a tool Keychain-viewer uses the following entitlments.

com.apple.keystore.access-keychain-keys

com.apple.keystore.device

1) Just upload this binary into your device in the /tmp folder and make sure its executable.

2) Now make sure that the keychain database file stored at the location /private/var/Keychains/keychain-2.db is world readable.

3) now go to terminal and you can dump your data by passing command

.keychain_dumper

4) above command will list down all the username and password. but above will only dump out the generic and internet passwords. You can see the usage information by using the “-h” command.

5) You can dump all the information using the “-a” command.

You can read more information and example over here dumping keychain data

Nik
  • 1,679
  • 1
  • 20
  • 36
0
  • Download and add keychainWrapper from here into your project.
  • Write following code in the viewController you want to reset keychain.

CODE:

#import "KeychainItemWrapper.h"

@interface YourViewController ()
{
    KeychainItemWrapper *keychainItemWrapper;
}

- (void)viewDidLoad {

    [super viewDidLoad];

    keychainItemWrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"appname" accessGroup:nil];

}

- (IBAction)logoutButtonPressed:(id)sender {

    [keychainItemWrapper resetKeychainItem];

}
mfaani
  • 33,269
  • 19
  • 164
  • 293
Khalid Usman
  • 1,272
  • 1
  • 13
  • 32
0

I needed to wipe out the entire user storage for my app, so used this:

NSMutableDictionary *storage = [[NSMutableDictionary alloc] init];
[storage setObject:"myService" forKey:(__bridge id)kSecAttrService];
[storage setObject:["myAccount" dataUsingEncoding:NSUTF8StringEncoding] forKey:(__bridge id)kSecAttrAccount];
// Possibly other attributes e.g.
[storage setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass];
[storage setObject:["somethingCustom" dataUsingEncoding:NSUTF8StringEncoding] forKey:(__bridge id)kSecAttrGeneric];
OSStatus status = SecItemDelete((CFDictionaryRef)storage);
// Handle status
// ...
timbre timbre
  • 12,648
  • 10
  • 46
  • 77