密码重置swift 4 firebase

时间:2018-02-05 21:26:30

标签: swift firebase firebase-authentication swift4

嘿伙计们,我正在使用Swift 4和Firebase进行注册。

我一直在重置密码。每当我点击按钮"忘记密码"我应该有一个带有textField的弹出窗口来填写我的电子邮件地址。但是当我点击时没有任何反应。如果有人有一些想法可能出错,可以在下面发布代码

@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)
            }
        })
    }))
}

2 个答案:

答案 0 :(得分:4)

作为快速回答,您只是忘记展示自己的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会自动执行此操作,但我不相信这种情况。此外,获取错误描述以呈现给用户的更好方法是使用可选绑定(使用if let)。

Auth.auth().sendPasswordReset(withEmail: resetEmail!, completion: { (error) in
    //Make sure you execute the following code on the main queue
    DispatchQueue.main.async {
        //Use "if let" to access the error, if it is non-nil
        if let error = error {
            let resetFailedAlert = UIAlertController(title: "Reset Failed", message: 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)
        }
    }
})

答案 1 :(得分:0)

快速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.")
                }
            }
        }
    }