卸载应用后注销用户-Firebase [英] Log user out after app has been uninstalled - Firebase

查看:87
本文介绍了卸载应用后注销用户-Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My app won't log out the current user after the app has been uninstalled from the phone. I don't want the user to uninstall the app, and when they reinstall it they are already logged in.

我认为它可能与钥匙串访问有关?没有把握.我在想,也许一旦删除该应用程序,我只需要取消对用户的身份验证即可,但是无法检查这种情况.与之最接近的是运行applicationWillTerminate函数,但是如果我将FIRAuth.auth()?.signOut()放在其中,则每次应用被终止时,它都会使我的用户退出.我不要

I think it has something to do with its keychain access maybe? Not sure. I was thinking maybe I needed to just un-authenticate the user once the app was deleted, but there is no way of checking that condition. The closest thing to that would be running the applicationWillTerminate function, but if I put my FIRAuth.auth()?.signOut() in there, it would sign my user out every time the app was killed. I don't want that.

我将如何进行这项工作?

推荐答案

虽然没有功能或处理程序可用来检查何时从手机上卸载了该应用程序,但我们可以检查它是否是该应用程序首次启动.首次启动应用程序时,很有可能意味着它已经安装并且在应用程序内未进行任何配置.此过程将在return true行上方的didfinishLaunchingWithOptions中执行.

While there is no function or handler for checking when the app has been uninstalled from the phone, we can check if it is the apps first launch. More than likely when an app is first launched, that also means it has just been installed and nothing has been configured within the app. This process will be executed in didfinishLaunchingWithOptions above the return true line.

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

First, we have to set up the User Defaults:

let userDefaults = UserDefaults.standard

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

After this, we need to check if the app has launched before or has been run before:

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
}

我们现在检查了是否是应用程序首次启动.现在,我们可以尝试注销我们的用户.这是更新后的条件的外观:

We have now checked if it is the apps first launch or not. Now we can try to log out our user. Here is how the updated conditional should look:

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
}

我们现在检查了用户是否是第一次启动该应用程序,如果是,则如果以前登录过,请注销该用户.所有组合的代码应如下所示:

We have now checked if the user is launching the app for the first time, and if so, log out a user if one was previously signed in. All the code put together should look like the following:

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
}

这篇关于卸载应用后注销用户-Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆