I'm developing an iOS app with Xcode and Swift.
I have a problem with this code:
@IBAction func login(sender: AnyObject) {
let request = NSMutableURLRequest(URL: NSURL(string: "http://myurl.com/contact/checklogin.php")!)
request.HTTPMethod = "POST"
let postString = "username=\(username.text!)&password=\(password.text!)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
dispatch_async(dispatch_get_main_queue(),{
if (responseString == "false") {
let alert = UIAlertView(title: "Error!", message: "Something is wrong. Please check this!", delegate: nil, cancelButtonTitle: "Okay")
alert.show()
}
else {
let alert = UIAlertView(title: "Successful!", message: "You'll get logged in now!", delegate: nil, cancelButtonTitle: "Okay")
alert.show()
}
}
})
task.resume()
}
When user logs in this code sends username and password to a PHP scipt on my server. This PHP script checks if this data/password pair really exist. If so it responses "true", else "false".
The problem is:
When user taps submit, (this code will run), the UI freezes for some seconds and the alert needs ca. 15 seconds to appear. But it's not a PHP script mistake, because print("responseString = \(responseString)")
prints immediately. I'm also getting this error message: https://codeshare.io/TGwe6
Does anybody know what I'm doing wrong?
P.S.
I know that many thinks are missing in the code (like hash instead of password or check for empty files). This is just an example.
P.P.S: Updated the code. It's working now.