【问题标题】:Log user out after app has been uninstalled - Firebase卸载应用后注销用户 - Firebase
【发布时间】:2017-04-05 14:54:04
【问题描述】:

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

我认为这可能与它的钥匙串访问有关?不确定。我在想也许我只需要在删除应用程序后取消对用户的身份验证,但是没有办法检查这种情况。最接近的方法是运行applicationWillTerminate 函数,但如果我将FIRAuth.auth()?.signOut() 放在那里,每次应用程序被终止时,它都会让我的用户退出。我不想那样。

我将如何进行这项工作?

【问题讨论】:

    标签: ios swift firebase firebase-authentication


    【解决方案1】:

    虽然没有用于检查应用程序何时从手机中卸载的函数或处理程序,但我们可以检查是否是应用程序首次启动。很可能在首次启动应用程序时,这也意味着它刚刚安装并且在应用程序中没有进行任何配置。此过程将在return true 行上方的didfinishLaunchingWithOptions 中执行。

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

    let userDefaults = UserDefaults.standard
    

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

    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
    }
    

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

    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
    }
    

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

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-19
      • 1970-01-01
      • 2015-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      相关资源
      最近更新 更多