gpt4 book ai didi

ios - 无论凭据是否正确,都会触发 Segue

转载 作者:行者123 更新时间:2023-11-28 07:12:03 26 4
gpt4 key购买 nike

我试图仅在凭据正确时才执行 segue。否则应该有一个警告说明“不正确的凭据”。当我运行应用程序时,segue 正在执行并转到下一个 View ,无论凭据是否正确。在 Storyboard 中,我已模态连接到 AccountViewController。为什么会发生这种情况?

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()
}}

最佳答案

您应该实现 shouldPerformSegueWithIdentifier:sender: 方法来控制是否要执行 segue。

关于ios - 无论凭据是否正确,都会触发 Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28081410/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com