AppsDev
AppsDev

Reputation: 12499

Deleting Keychain when uninstalling iOS app

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?

Upvotes: 4

Views: 5593

Answers (2)

hooliooo
hooliooo

Reputation: 548

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.

Upvotes: 6

Tung Fam
Tung Fam

Reputation: 8147

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!

Upvotes: 2

Related Questions