Search code examples
ioslifecyclekeychain

Delete keychain data on iOS


I have an iOS application that uses the keychain to store some information related to the authentication. I would like to remove this data from the keychain when the application is uninstalled. How can I get aware of the application being uninstalled/removed?


Solution

  • You can´t detect when your application is going to be deleted from your device. But you could detect when your app is launched for first time, after to be installed. For this I use the next code:

    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunched"])
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunched"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    
        //Here delete your info kept in your keychain. If not exist will not delete nothing, but if it exist mean your app has been installed again.
        [removeYourPropertyInKeychain];
    }