Ad

Trouble Implementing UIAlertController. Cant Get Alert To Show Up

I have set an alert to be shown as any error occurs when some user tries to request a new password through firebase, but it is not working.

The print("problems with email field") is being printed so I believe I have made something wrong when writing the alert part.

@IBAction func recuperarSenha(_ sender: Any) {

    Auth.auth().sendPasswordReset(withEmail: self.loginTextView.text!) { error in
        if error != nil {
            print("problems with email field")

            let alert = UIAlertController(title: "Couldn't send recover message", message: "Check if e-mail field is properly filled.", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK!", style: .default, handler: nil))

        }
    }
}
Ad

Answer

You need to present your alert after creating it. Add the following code after adding action:

self.present(alert, animated: true, completion: nil)

Edited version of your code:

@IBAction func recuperarSenha(_ sender: Any) {

  Auth.auth().sendPasswordReset(withEmail: self.loginTextView.text!) { error in
    if error != nil {
        print("problems with email field")

        let alert = UIAlertController(title: "Couldn't send recover message", message: "Check if e-mail field is properly filled.", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK!", style: .default, handler: nil))
        self.present(alert, animated: true, completion: nil)


    }
  }
}
Ad
source: stackoverflow.com
Ad