首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >密码重置快速4火力基地

密码重置快速4火力基地
EN

Stack Overflow用户
提问于 2018-02-05 21:26:30
回答 2查看 4.9K关注 0票数 0

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

我被重新设置密码困住了。每当我点击按钮“忘记密码”,我应该得到一个弹出窗口与textField,以填写我的电子邮件地址。但当我点击的时候什么都没发生。在下面张贴代码,如果有人有一些想法,它可能有什么问题

代码语言:javascript
复制
@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)
            }
        })
    }))
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-05 21:55:43

作为一个快速的回答,你只是忘了展示你的forgotPasswordAlert。修复方法很简单:

代码语言:javascript
复制
@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)。

代码语言:javascript
复制
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)
        }
    }
})
票数 7
EN

Stack Overflow用户

发布于 2019-04-04 05:49:04

For swift 4.2

代码语言:javascript
复制
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.")
                }
            }
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48631755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档