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

标签 ios swift firebase firebase-authentication

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

我认为这可能与它的钥匙串(keychain)访问有关?没有把握。我在想也许我需要在删除应用程序后取消对用户的身份验证,但无法检查该情况。最接近的是运行 applicationWillTerminate 函数,但如果我将 FIRAuth.auth()?.signOut() 放在那里,它会注销我的用户每次应用程序被杀死。我不想要那个。

我将如何进行这项工作?

最佳答案

虽然没有用于检查应用程序何时从手机上卸载的函数或处理程序,但我们可以检查它是否是应用程序首次启动。当一个应用程序首次启动时,这很可能也意味着它刚刚安装并且没有在应用程序中进行任何配置。此过程将在 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
}

关于ios - 卸载应用程序后注销用户 - Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40733262/

相关文章:

ios - 添加带动画的约束查看

iphone - 禁用 iOS 音量按钮快门

cocoa-touch - Xcode 提示使用 Swift 和 Xcode 6 的 if 语句结构

ios - 从图层重新加载 tableView 时崩溃

ios - 在 Swift 函数之间传递索引值的最佳方式

java - Android 上的 Firebase 自动重新连接

java - 如何防止 Firebase 用户登录到错误的登录屏幕

ios - 创建自定义调出 View

php - php返回的数据为空,iOS

json - 完美的JSON结构