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

查看:14
本文介绍了卸载应用后注销用户 - 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天全站免登陆