Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function that detects an app uninstall/reinstall

Tags:

swift

swift3

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
like image 899
Sylar Avatar asked Mar 22 '26 12:03

Sylar


1 Answers

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.

like image 69
Jacob King Avatar answered Mar 24 '26 01:03

Jacob King



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!