I have a log in function in my app that I'm creating and it seemed like it was working fine. I use Parse. Here is the login function.
@IBAction func login(_ sender: AnyObject) {
if self.userName.text == "" || self.password.text == "" {
self.createAlert(title: "Login Error", message: "Please make sure all fields are filled in")
}else{
PFUser.logInWithUsername(inBackground: userName.text!, password: password.text!) { (user, error) in
self.activityIndicator.stopAnimating()
UIApplication.shared.endIgnoringInteractionEvents()
if error != nil {
var displayErrorMessage = "Please try again later."
if let errorMessage = (error! as NSError).userInfo["error"] as? String {
displayErrorMessage = errorMessage
}
self.createAlert(title: "Signup Error", message: displayErrorMessage)
}else{
print("logged in")
self.performSegue(withIdentifier: "successLogin", sender: self)
}
}
}
}
So the first check checks to see if the username or password textfields are blank and if so, it creates an alert. When i first start the app, it works like a charm. However when I log in with a user and log out, I'm redirected to the login screen where the username and password are blank. And i click login and get the proper alert saying "Please make sure all fields are filled in" and when I dismiss it, it automatically performs the segue as though the login was successful. Can anyone please help me?
@IBAction func logOut(_ sender: AnyObject) {
PFUser.logOut()
performSegue(withIdentifier: "logOutSeg", sender: self)
}
Also, this is my create alert function:
func createAlert(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
self.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated: true, completion: nil)
}
If i remove the createAlert method after the first if statement, it does things correctly so I'm assuming it's in that.
When you're logging out of 2nd screen for the first time, if it comes to first logging screen by performing a segue, this could happen. That's why you get empty username and password. When you dismiss the alertviewcontroller in login screen by calling self.dismiss(animated: true, completion: nil)
, it dismisses the current view controller. Dismiss the logged in screen (2nd screen) instead of performing segue after logging out.