3

I have been reading several posts regarding this issue, for example Delete keychain items when an app is uninstalled and iOS autodelete Keychain items after uninstall?. They say that, when you uninstall an app, its Keychain is not deleted, but the posts may be deprecated, is that the current behaviour?

On the other hand, if Keychain is not really automatically deleted when the user uninstalls an app, I'm not clear about the way to do that yourself.

EDIT: If Keychain are not deleted when apps are uninstalled, what actually happens to all those residual Keychain? Does the system not handle that?

Community
  • 1
  • 1
AppsDev
  • 12,319
  • 23
  • 93
  • 186

2 Answers2

5

Try using UserDefaults to store a boolean that tracks when data is saved to the keychain.

Example:

func someFunctionThatSavesToKeychain {
    // Save to keychain
    UserDefaults.standard.set(true, forKey: "isSavedToKeychain")
    // Do other stuff
}

Then in AppDelegate in the didFinishLaunchingWithOptionsMethod

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    if !UserDefaults.standard.bool(forKey: "isSavedToKeychain") {
        // Delete data from Keychain
    }
}

Since UserDefaults is cleared on application uninstall, the next time the user installs the application, that key-value will be gone therefore on start up, your AppDelegate will delete the residual Keychain data.

I've searched far and wide as well, this workaround is the closest you can get.

hooliooo
  • 528
  • 1
  • 5
  • 13
  • Thanks. But this only will clear the `Keychain` if the user installs the app again, right? What happens if she/he doesn't? – AppsDev Dec 12 '16 at 13:13
  • Yes. If the user doesn't then the data stays in Keychain. If you're worried about the amount of space your data will take up in the Keychain, maybe Keychain isn't the place to save it. An alternative could be in the App Directory. – hooliooo Dec 12 '16 at 13:36
  • great idea! I'm ignoring the KeyChain value if flag is false or not found – orafaelreis Oct 26 '18 at 19:49
  • This will not work if a user has multiple apps from the same Developer, as user defaults are only deleted when all apps are deleted from a developer -> https://stackoverflow.com/questions/24985825/nsuserdefaults-not-cleared-after-app-uninstall-on-simulator – Peter Suwara May 01 '19 at 15:12
2

There is no trigger to perform code when the app is deleted from the device. Access to the keychain is dependent 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.

I don't think you need to delete it. I'm not sure how to delete it but I believe if you did set the keychain value to some certain then you can also assign the value of nil or just empty string "". But this is not quite sure, just assuming.

Hope it helps!

Tung Fam
  • 7,899
  • 4
  • 56
  • 63