我目前正在开发一个带有登录页面的应用程序,该页面带有登录按钮和主菜单按钮。我已经创建了所有内容,并且暂时运行良好。我正在使用Firebase作为后端数据库。
我想创建一个“忘记密码”按钮,该按钮我已经做过并且似乎可以正常使用,但是唯一的问题是它在发送电子邮件时没有给我警报,它只是发送了电子邮件,但我得到了当我输入不在用户身份验证中的电子邮件时会发出警报。以下是我的“忘记密码”按钮的代码:
@IBAction func frgtpassTapped(_ sender: Any) {
Auth.auth().sendPasswordReset(withEmail: emailTextField.text!) { error in
if self.emailTextField.text?.isEmpty==true{
let resetFailedAlert = UIAlertController(title: "Reset Failed", message: "Error: \(String(describing: error?.localizedDescription))", preferredStyle: .alert)
resetFailedAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(resetFailedAlert, animated: true, completion: nil)
}
if error != nil && self.emailTextField.text?.isEmpty==false{
let resetEmailAlertSent = UIAlertController(title: "Reset Email Sent", message: "Reset email has been sent to your login email, please follow the instructions in the mail to reset your password", preferredStyle: .alert)
resetEmailAlertSent.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(resetEmailAlertSent, animated: true, completion: nil)
}
}
}
我接受是否有人对这篇文章投反对票,但是如果您反对,请在原因下方评论。任何帮助将非常感激!
答案 0 :(得分:0)
Please this code
Auth.auth().sendPasswordReset(withEmail: emailTextField.text!) { error in
DispatchQueue.main.async {
if self.emailTextField.text?.isEmpty==true || error != nil {
let resetFailedAlert = UIAlertController(title: "Reset Failed", message: "Error: \(String(describing: error?.localizedDescription))", preferredStyle: .alert)
resetFailedAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(resetFailedAlert, animated: true, completion: nil)
}
if error == nil && self.emailTextField.text?.isEmpty==false{
let resetEmailAlertSent = UIAlertController(title: "Reset Email Sent", message: "Reset email has been sent to your login email, please follow the instructions in the mail to reset your password", preferredStyle: .alert)
resetEmailAlertSent.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(resetEmailAlertSent, animated: true, completion: nil)
}
}
}
答案 1 :(得分:0)
从代码中很明显,您正在将unWrapped值传递给闭包,因此textField中必须包含一些值。
您的代码中的这一行:如果self.emailTextField.text?.isEmpty == true {
将永远不会输入内部条件。 它应该更新为:
@IBAction func frgtpassTapped(_ sender: Any) {
//BEFORE SENDING DATA FOR AUTHENTICATION, CHECK FOR THE isEmpty CONDITIONS HERE
Auth.auth().sendPasswordReset(withEmail: emailTextField.text!) { error in
DispatchQueue.main.async {
if error != nil {
// YOUR ERROR CODE
} else {
//YOUR SUCCESS MESSAGE
}
}
}
}