嘿,伙计们,我正在用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)
}
})
}))
}发布于 2018-02-05 21:55:43
作为一个快速的回答,你只是忘了展示你的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)
}
}
})发布于 2019-04-04 05:49:04
For 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.")
}
}
}
}https://stackoverflow.com/questions/48631755
复制相似问题