Home>
When you press the login button, I want to display "Login successful" if the login is successful, or "Login failed" if the login fails.

Please write your question in detail here.
I am a swift beginner.
We aim to implement a login function using firebase.
Currently, the login screen allows you to log in after entering your email and password.
However, even if you log in, there is no change in the screen, so if you look from the simulator
I don't know if I'm logged in, so I want to display an alert message.
Therefore, I want to display different alerts depending on whether I can log in or not.
I've looked into it, but I'm not sure where to put the necessary code.
Probably I use UIAlertController, but all of them are articles that display an alert when the button is pressed, and if you can log in by pressing the button, A,
When I couldn't log in, I couldn't find a solution like B.
Thanks for your cooperation.

Error message
If i can log in after pressing the login button
If i am unable to log in, "I want to display a message that I could not log in.</Code></pre>
<strong>Applicable source code</strong>
<pre><code>@IBAction func loginTapped (_ sender: Any) {
        Auth.auth (). SignIn (withEmail: email.text !, password: password.text!) {(User, error) in
      If error! = Nil {
        Print ("Login failed")
                   } else {
                       print ("You have successfully logged in")
                   }
               }
    }

if error! = nil {
func displayAlert () {
_ = "Login failed"
_ = "Please check your email address and password again and press the login button"
_ = "ok"
}

print ("Login failed")

}
I tried to describe it, but the simulator did not change, just saying "I could not log in" on the console.

Supplemental information (FW/tool version etc.)

Xcode 11.2.1
Swift version 5.1.2

  • Answer # 1

    func showAlert () {
    let title = "Login failed"
    let message = "Your email address or password is incorrect"
    let okText = "ok"

    let alert = UIAlertController (title: title, message: message, preferredStyle: UIAlertController.Style.alert)
    let okayButton = UIAlertAction (title: okText, style: UIAlertAction.Style.cancel, handler: nil)
    alert.addAction (okayButton)

    present (alert, animated: true, completion: nil)
    }

    define showAlert method
    If you call with "self.showAlert ()" in the button action part, the alert was displayed safely.
    Was self resolved.