Delete keychain items when an app is uninstalled

By : enc

I am using idandersen's scifihifi-iphone code for keychain and save password using

[SFHFKeychainUtils storeUsername:@"User" andPassword:@"123"
              forServiceName:@"TestService" updateExisting:YES error:&error];

When I delete the application from the device, the password remains in the keychain.

I want to remove the password from the keychain when the user deletes the application from the device. How can I do this?

By : enc


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

let userDefaults = UserDefaults.standard

if userDefaults.bool(forKey: "hasRunBefore") == false {
     // Remove Keychain items here

     // Update the flag indicator
     userDefaults.set(true, forKey: "hasRunBefore")
     userDefaults.synchronize() // Forces the app to update UserDefaults

Like other answers, I would suggest using NSUserDefaults.

Pretty much everything was said, but for the sake of universal knowledge and wisdom, here's an alternative on clearing the keychain on first launch: generate a random string that you'll append to your bundle identifier, and store it in NSUserDefaults. That way, if the user reinstalls the app, old passwords will still be there but shouldn't be picked up by the app - as the service name will be different.

But... really, just wipe everything on first launch and you should be fine.

Files will be deleted from your app's document directory when the user uninstalls the app. Knowing this, all you have to do is check whether a file exists as the first thing that happens in application:didFinishLaunchingWithOptions:. Afterwards, unconditionally create the file (even if it's just a dummy file).

If the file did not exist at time of check, you know this is the first run since the latest install. If you need to know later in the app, save the boolean result to your app delegate member.

By : stephen

This video can help you solving your question :)
By: admin