在卸载应用程序后将用户注销 - Firebase

bwc*_*ley 7 ios firebase swift firebase-authentication

从手机上卸载应用程序后,我的应用程序不会注销当前用户.我不希望用户卸载该应用程序,当他们重新安装时,他们已经登录.

我认为这与其钥匙串访问有关吗?不确定.我想也许我需要在删除应用程序后取消对用户进行身份验证,但无法检查该情况.最接近的是运行该applicationWillTerminate函数,但如果我把FIRAuth.auth()?.signOut()它放在那里,它会在每次应用程序被杀时签署我的用户.我不希望这样.

我该怎么做才能做到这一点?

bwc*_*ley 20

虽然没有功能或处理程序来检查从手机上卸载应用程序的时间,但我们可以检查是否是应用程序首次启动.首次启动应用程序的可能性很大,这也意味着它刚刚安装好,并且在应用程序中没有配置任何内容.该过程将在didfinishLaunchingWithOptionsreturn true行上方执行.

首先,我们必须设置用户默认值:

let userDefaults = UserDefaults.standard
Run Code Online (Sandbox Code Playgroud)

在此之后,我们需要检查应用程序之前是否已启动或之前是否已启动:

if (!userDefaults.bool(forKey: "hasRunBefore")) {
    print("The app is launching for the first time. Setting UserDefaults...")

    // Update the flag indicator
    userDefaults.set(true, forkey: "hasRunBefore")
    userDefaults.synchronize() // This forces the app to update userDefaults

    // Run code here for the first launch

} else {
    print("The app has been launched before. Loading UserDefaults...")
    // Run code here for every other launch but the first
}
Run Code Online (Sandbox Code Playgroud)

我们现在检查是否是首次启动的应用程序.现在我们可以尝试注销我们的用户.以下是更新后的条件应如何显示:

if (!userDefaults.bool(forKey: "hasRunBefore")) {
    print("The app is launching for the first time. Setting UserDefaults...")

    do {
        try FIRAuth.auth()?.signOut()
    } catch {

    }

    // Update the flag indicator
    userDefaults.set(true, forkey: "hasRunBefore")
    userDefaults.synchronize() // This forces the app to update userDefaults

    // Run code here for the first launch

} else {
    print("The app has been launched before. Loading UserDefaults...")
    // Run code here for every other launch but the first
}
Run Code Online (Sandbox Code Playgroud)

我们现在已经检查过用户是否第一次启动应用程序,如果是,请注销用户(如果之前已登录过).所有代码放在一起应如下所示:

let userDefaults = UserDefaults.standard

if (!userDefaults.bool(forKey: "hasRunBefore")) {
    print("The app is launching for the first time. Setting UserDefaults...")

    do {
        try FIRAuth.auth()?.signOut()
    } catch {

    }

    // Update the flag indicator
    userDefaults.set(true, forkey: "hasRunBefore")
    userDefaults.synchronize() // This forces the app to update userDefaults

    // Run code here for the first launch

} else {
    print("The app has been launched before. Loading UserDefaults...")
    // Run code here for every other launch but the first
}
Run Code Online (Sandbox Code Playgroud)