Sylar
Sylar

Reputation: 12082

Function that detects an app uninstall/reinstall

Been looking around to find a way to detect when an app is uninstalling/reinstalling. The thing is, I do not use NSUserDefaults, I use SwiftKeychainWrapper.

I need to clear that user's keychain when app uninstalls.

didFinishLaunchingWithOptions seems to call when app loads so no use there. Is there a way to detect a reinstall? I need to call this:

return KeychainWrapper.standard.removeObject(forKey: "myKey") // only when/if app is unsinstalled/reinstalling

Upvotes: 3

Views: 2766

Answers (2)

Apps-n-Add-Ons
Apps-n-Add-Ons

Reputation: 2146

for others searching for the way to clear the Keychain in the "// delete your item" part of the answer.....

Swift 3

let _ = KeychainWrapper.standard.removeAllKeys()

Upvotes: 1

Jacob King
Jacob King

Reputation: 6157

Presumably you're using Keychain because you need to store sensitive information? If so then you could just store a boolean in UserDefaults and check if that exists. For example:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let freshInstall = !UserDefaults.standard.bool(forKey: "alreadyInstalled")
    if freshInstall {
        // delete your item from keychain
        UserDefaults.standard.set(true, forKey: "alreadyInstalled")
    }

    return true
}

This way you still have the security of the Keychain, but the behaviour of UserDefaults on uninstall/reinstall.

Upvotes: 3

Related Questions