gpt4 book ai didi

ios - Swift Firebase 在应用程序终止时删除用户

转载 作者:行者123 更新时间:2023-11-29 00:17:21 31 4
gpt4 key购买 nike

我试图在应用程序终止时删除用户,但这不起作用。如果我有一个匿名用户登录并且用户关闭了应用程序,我不希望 firebase 保存匿名用户。我想删除它。这是我当前的代码。提前谢谢你

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
if let users = Auth.auth().currentUser {
if !users.isAnonymous {
return
}else {
users.delete(completion: { (error) in
if error != nil {
print(error!)
return
}
try! Auth.auth().signOut()
print("Anonymous user deleted")
})
}
}
}

最佳答案

我将 ViewDidLoad 中的监听器添加到我希望从中删除用户的屏幕。像这样:

    NotificationCenter.default.addObserver(self, selector: #selector(suspending), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(suspending), name: NSNotification.Name.UIApplicationWillTerminate, object: nil)

然后我有这个功能来删除用户:

  @objc func suspending () {
if Auth.auth().currentUser != nil {
let user = Auth.auth().currentUser
user?.delete { error in
if let error = error {
print("An error happened delting the user.")
} else {
print("Account succesfully deleted.")
}
}
}
}

唯一可能发生的事情是,您需要重新进行身份验证。如果是这种情况,请遵循 this answer实现它也是如此

关于ios - Swift Firebase 在应用程序终止时删除用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44999153/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com