0

For my application, it requires the user to login. If the user login is invalid, I need to let the user know that. So I was wondering how to bring up an UIAlertView if the login parameters are invalid. It automatically lets me know in the terminal if its valid or invalid. But I need to know how to create an AlertView so the user knows they entered their information successfully or unsuccessfully.

CODE

IBAction func loginTapped(sender: AnyObject) {

    let username = usernameField.text
    let password = passwordField.text

    if username.isEmpty || password.isEmpty {
        var emptyFieldsError:UIAlertView = UIAlertView(title: "Please try again", message: "Please fill in all the fields we can get you logged in to your account.", delegate: self, cancelButtonTitle: "Try again")
        emptyFieldsError.show()
    }

    PFUser.logInWithUsernameInBackground(username, password:password) {
        (user: PFUser?, error: NSError?) -> Void in
        if user != nil {
            println("Sucessful")
        } else {
            println("Invalid login")
        }
    }

}

1 Answer 1

1

try this.. :

 // Mark: Error message alert.

var errorMessage = String()

func alertView(title: String, message: String, buttonName: String){

    var alert:UIAlertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: buttonName, style: UIAlertActionStyle.Default, handler: { (action) -> Void in

        self.dismissViewControllerAnimated(true, completion: nil)

    }))

    self.presentViewController(alert, animated: true, completion: nil)
}


@IBAction func loginTapped(sender: AnyObject) {

    PFUser.logInWithUsernameInBackground(userName.text, password: password.text) { (user: PFUser?, error: NSError?) -> Void in

        if user != nil {

            // user logged in.. Perform Segue here..

            self.performSegueWithIdentifier("a", sender: self)

        } else {

            // Show error here..

            if let errorString = error!.userInfo?["error"] as? String {
                self.errorMessage = errorString

            }

            self.alertView("Error!", message: self.errorMessage, buttonName: "Try again")

        }


    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.