检测应用程序卸载/重新安装的功能

时间:2017-02-13 16:13:39

标签: swift swift3

looking around找到检测应用何时卸载/重新安装的方法。问题是,我不使用NSUserDefaults,我使用SwiftKeychainWrapper

我需要在卸载应用时清除该用户的钥匙串。

didFinishLaunchingWithOptions似乎在应用加载时调用,因此没有用处。有没有办法检测重新安装?我需要称之为:

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

2 个答案:

答案 0 :(得分:2)

您可能因为需要存储敏感信息而使用Keychain?如果是这样,那么你可以在UserDefaults中存储一个布尔值并检查它是否存在。例如:

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
}

这样你仍然拥有Keychain的安全性,但UserDefaults在卸载/重新安装时的行为。

答案 1 :(得分:1)

其他人在“//删除你的项目”部分答案中寻找清除钥匙串的方法......

Swift 3

let _ = KeychainWrapper.standard.removeAllKeys()
相关问题