密码重置Swift 4 Firebase

bas*_*sis 0 firebase swift 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)
            }
        })
    }))
}
Run Code Online (Sandbox Code Playgroud)

cre*_*eak 5

作为快速回答,您只是忘了出示您的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)
}
Run Code Online (Sandbox Code Playgroud)

另外,您需要确保在主队列上显示确认警报,以避免意外行为。也许这样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)
        }
    }
})
Run Code Online (Sandbox Code Playgroud)