虽然没有功能或处理程序来检查应用程序何时从手机上卸载,但我们可以检查它是否是应用程序第一次启动。当应用程序首次启动时,这意味着它刚安装完毕,应用程序中没有任何配置。该过程将在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
}