1

I am attempting to only perform a segue if credentials are correct. Otherwise there should be an Alert stating "incorrect credentials". When I run the app the segue is performing and going to the next view whether credentials are correct/incorrect. In the Storyboard I have connected to the AccountViewController modally. Why might this be happening?

class ViewController: UIViewController, NSURLConnectionDelegate {


@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!

var token: NSString = ""


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        var accountViewController = segue.destinationViewController as AccountViewController
        accountViewController.token = self.token

}

private let api_key = ""

func logIn() {


    var email: NSString = emailTextField.text
    var pass: NSString = passwordTextField.text

    // Remove characters from the custom character set with the custom set

    let customEncodedSet: NSMutableCharacterSet = NSCharacterSet.URLHostAllowedCharacterSet().mutableCopy() as NSMutableCharacterSet
    customEncodedSet.removeCharactersInString("!*'();:@&=+$,/?%#[]")

        var encodedEmail = email.stringByAddingPercentEncodingWithAllowedCharacters(customEncodedSet)
        var encodedPass = pass.stringByAddingPercentEncodingWithAllowedCharacters(customEncodedSet)


    var url = "https://www.photoshelter.com/psapi/v3/mem/authenticate?api_key=\(api_key)&email=\(encodedEmail!)&password=\(encodedPass!)&mode=token"


    var baseURL:NSURL? = NSURL(string: url)
    var request: NSMutableURLRequest? = NSMutableURLRequest(URL: baseURL!)
    var session = NSURLSession.sharedSession()
    var task = session.dataTaskWithRequest(request!, completionHandler: { (data, response, error) -> Void in
        dispatch_async(dispatch_get_main_queue(), { () -> Void in


            if response == nil {
                UIAlertView(title: "PhotoShelter", message: "No internet connection", delegate: nil, cancelButtonTitle: "Cancel").show()
            } else {
                var responseObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil)
                var status: AnyObject? = responseObject?.objectForKey("status")

                if status as String != "ok" {
                    UIAlertView(title: "PhotoShelter", message: "Invalid Credentials", delegate: nil, cancelButtonTitle: "OK").show()

                } else {

                    var tokenString: AnyObject? = responseObject?.objectForKey("data")
                    self.token = tokenString?.objectForKey("token") as NSString
                    self.performSegueWithIdentifier("loginSuccessSegue", sender: self)
                }

            }

        })
    })
    task.resume()
}


@IBAction func signInPressed(sender: AnyObject) {

    if self.emailTextField.text == "" || self.passwordTextField.text == "" {
        UIAlertView(title: "PhotoShelter", message: "Empty Username or Password", delegate: nil, cancelButtonTitle: "OK").show()


    } else {
        logIn()
    }}

1 Answers1

1

You should implement shouldPerformSegueWithIdentifier:sender: method to control whether you want the segue to be performed.

gabbler
  • 13,626
  • 4
  • 32
  • 44
  • @LyndseyScott, I only see `prepareForSegue` method, am I wrong? – gabbler Jan 22 '15 at 04:52
  • Yeah, shouldPerformSegueWithIdentifier:sender: is within login()'s dataTaskWithRequestBlock and called if the status == ok – Lyndsey Scott Jan 22 '15 at 04:54
  • I don't see `shouldPerformSegueWithIdentifier` either, however the logic in the block should be sufficient - Lyndsey's other comment is likely the issue - a direct link to the segue from the button in addition to the @IBAction – Paulw11 Jan 22 '15 at 05:00
  • Oh, yeah, you're both right. I wasn't paying attention, sorry. I meant performSegueWithIdentifier. – Lyndsey Scott Jan 22 '15 at 05:10