Delete keychain items when an app is uninstalled Delete keychain items when an app is uninstalled ios ios

Delete keychain items when an app is uninstalled


You can take advantage of the fact that NSUserDefaults are cleared by uninstallation of an app. For example:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    //Clear keychain on first run in case of reinstallation    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"FirstRun"]) {        // Delete values from keychain here        [[NSUserDefaults standardUserDefaults] setValue:@"1strun" forKey:@"FirstRun"];        [[NSUserDefaults standardUserDefaults] synchronize];    }    //...Other stuff that usually happens in didFinishLaunching}

This checks for and sets a "FirstRun" key/value in NSUserDefaults on the first run of your app if it's not already set. There's a comment where you should put code to delete values from the keychain. Synchronize can be called to make sure the "FirstRun" key/value is immediately persisted in case the user kills the app manually before the system persists it.


For users looking for a Swift 3.0 version of @amro's answer:

let userDefaults = UserDefaults.standardif !userDefaults.bool(forKey: "hasRunBefore") {     // Remove Keychain items here     // Update the flag indicator     userDefaults.set(true, forKey: "hasRunBefore")}

*note that synchronize() function is deprecated


There is no trigger to perform code when the app is deleted from the device. Access to the keychain is dependant on the provisioning profile that is used to sign the application. Therefore no other applications would be able to access this information in the keychain.

It does not help with you aim to remove the password in the keychain when the user deletes application from the device but it should give you some comfort that the password is not accessible (only from a re-install of the original application).