0

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.

  • 1) Never do UI work on a background thread. 2) `UIAlertView` is deprecated. – rmaddy Aug 25 '16 at 14:24
  • 2) So, what to do? –  Aug 25 '16 at 14:26
  • Read the documentation for `UIAlertView`, it will tell you what to use instead. – rmaddy Aug 25 '16 at 14:28
  • Also here: http://stackoverflow.com/questions/28302019/getting-a-this-application-is-modifying-the-autolayout-engine-error. Did you even try to search for the error message "This application is modifying the autolayout engine from a background thread" ? – Martin R Aug 25 '16 at 14:30
  • Oh, you're right I didn't because in my "whole" code there more outputs and I didn't see that. Sorry! –  Aug 25 '16 at 14:32

1 Answers1

1

You need to show alert on main thread, always perform UI changes on main thread like this.

dispatch_async(dispatch_get_main_queue(),{
     //Show alert
}) 

Note: You need to use UIAlertController instead of UIAlertView because it is deprecated.

There are many tutorial for AlertController, some of these are as below.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • You're a hero. Thank you very much. It's working. I'll have to wait 8 minutes to accept your answer. –  Aug 25 '16 at 14:26