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