首页 > 解决方案 > 在删除帐户之前发出“你确定吗”警报

问题描述

我有一个使用 Firebase 数据库的应用程序。我还有一个连接到按钮的功能来删除他们的 firebase 帐户,但我需要发出某种警报来询问用户“你确定吗?”。

删除功能:

let user = Auth.auth().currentUser
    let firebase = Database.database().reference()

    // ... removes auth account

    user?.delete(completion: { (error) in
        if let error = error {
            print(error.localizedDescription)
        } else {
            print("Auth Deleted")
        }
    })


    // ... removes account from database

    firebase.child("Users/Riders").child((user?.uid)!).removeValue { (error, ref) in
        if error != nil {
            print("error \(String(describing: error))")
        } else {
            print("Database acct deleted!")
        }
    }

    self.logOutAction(self)

我现在需要创建一个警报,当用户按下删除按钮时,会弹出一个警报,其中包含 2 个选项是或否。如果他们按下是,“删除”功能将被激活,如果他们按下否,它会取消操作。

我知道如何创建警报,只是不知道如何在用户按下“是”时实现“删除”功能

标签: iosswiftfirebase-realtime-database

解决方案


在运行此删除功能之前,您可以查看UIAlertController来完成此操作。您可以为实际调用删除函数的确认操作设置处理程序。确保从主线程运行它。

例如,连接到删除按钮的函数可能如下所示:

let alert = UIAlertController(title: "Delete Account", message: "Are you sure you want to delete this account?", preferredStyle: .alert)

let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: { (action) in
    print("Call the deletion function here")
    self.processDelete()
})
alert.addAction(deleteAction)

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
alert.addAction(cancelAction)

self.present(alert, animated: true, completion: nil)

推荐阅读