2016-11-22 86 views
5

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

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

我该如何去做这项工作?

回答

12

虽然没有功能或处理程序来检查应用程序何时从手机上卸载,但我们可以检查它是否是应用程序第一次启动。当应用程序首次启动时,这意味着它刚安装完毕,应用程序中没有任何配置。该过程将在return true行之上的didfinishLaunchingWithOptions中执行。

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

let userDefaults = UserDefaults.standard 

在此之后,我们需要检查,如果应用程序之前已经推出或之前已运行:

if userDefaults.bool(forKey: "hasRunBefore") == false { 
    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") == false { 
    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") == false { 
    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 
} 
相关问题