密码重置swift 4 firebase

密码重置swift 4 firebase,swift,firebase,firebase-authentication,swift4,Swift,Firebase,Firebase Authentication,Swift4,嘿,伙计们,我正在用Swift 4和Firebase做登记表 我无法重新设置密码。每当我点击“忘记密码”按钮时,我就会弹出一个带有文本字段的窗口来填写我的电子邮件地址。但当我点击时,什么也没发生。如果有人有什么想法,请在下面发布代码 @IBAction func forgotPasswordTapped(_ sender: Any) { let forgotPasswordAlert = UIAlertController(title: "Forgot password?", messa

嘿,伙计们,我正在用Swift 4和Firebase做登记表

我无法重新设置密码。每当我点击“忘记密码”按钮时,我就会弹出一个带有文本字段的窗口来填写我的电子邮件地址。但当我点击时,什么也没发生。如果有人有什么想法,请在下面发布代码

@IBAction func forgotPasswordTapped(_ sender: Any) {
    let forgotPasswordAlert = UIAlertController(title: "Forgot password?", message: "Enter email address", preferredStyle: .alert)
    forgotPasswordAlert.addTextField { (textField) in
        textField.placeholder = "Enter email address"
    }
    forgotPasswordAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    forgotPasswordAlert.addAction(UIAlertAction(title: "Reset Password", style: .default, handler: { (action) in
        let resetEmail = forgotPasswordAlert.textFields?.first?.text
        Auth.auth().sendPasswordReset(withEmail: resetEmail!, completion: { (error) in
            if 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)
            }else {
                let resetEmailSentAlert = UIAlertController(title: "Reset email sent successfully", message: "Check your email", preferredStyle: .alert)
                resetEmailSentAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                self.present(resetEmailSentAlert, animated: true, completion: nil)
            }
        })
    }))
}

作为快速回答,您只是忘记了显示您的
forgotPasswordAlert
。解决方法很简单:

@IBAction func forgotPasswordTapped(_ sender: Any) {
    let forgotPasswordAlert = UIAlertController(title: "Forgot password?", message: "Enter email address", preferredStyle: .alert)
    forgotPasswordAlert.addTextField { (textField) in
        textField.placeholder = "Enter email address"
    }
    forgotPasswordAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    forgotPasswordAlert.addAction(UIAlertAction(title: "Reset Password", style: .default, handler: { (action) in
        let resetEmail = forgotPasswordAlert.textFields?.first?.text
        Auth.auth().sendPasswordReset(withEmail: resetEmail!, completion: { (error) in
            if 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)
            }else {
                let resetEmailSentAlert = UIAlertController(title: "Reset email sent successfully", message: "Check your email", preferredStyle: .alert)
                resetEmailSentAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                self.present(resetEmailSentAlert, animated: true, completion: nil)
            }
        })
    }))
    //PRESENT ALERT
    self.present(forgotPasswordAlert, animated: true, completion: nil)
}
另外,您需要确保在主队列上显示确认警报,以避免出现意外行为。也许
sendPasswordReset
会自动执行此操作,但我不认为是这样。此外,向用户显示错误描述的更好方法是使用可选绑定(如果let,则使用


适用于swift 4.2

import Firebase

// MARK: Firebase Forgotpassword
    func callFIRPasswordReset(){
        //show loader

        Auth.auth().sendPasswordReset(withEmail: self.txtEmail.text!) { (error) in
            DispatchQueue.main.async {
                //hide loader

                self.txtEmail.text = ""

                if let error = error {
                    //show alert here
                    print(error.localizedDescription)
                }
                else {
                    //show alert here
                    print("We send you an email with instructions on how to reset your password.")
                }
            }
        }
    }

非常感谢!这是我犯的愚蠢错误,从斯威夫特开始,希望能变得更好,哈哈
import Firebase

// MARK: Firebase Forgotpassword
    func callFIRPasswordReset(){
        //show loader

        Auth.auth().sendPasswordReset(withEmail: self.txtEmail.text!) { (error) in
            DispatchQueue.main.async {
                //hide loader

                self.txtEmail.text = ""

                if let error = error {
                    //show alert here
                    print(error.localizedDescription)
                }
                else {
                    //show alert here
                    print("We send you an email with instructions on how to reset your password.")
                }
            }
        }
    }