0

I am trying to get login/register on my iOS app to work. I am using mySQL and PHP besides Swift 2.0. For some reason when I try to send my HTTP POST to the PHP-scripts I keep geting the error: "The data couldn’t be read because it isn’t in the correct format."

I am using MAMP for the mySQL server.

let request = NSMutableURLRequest(URL: NSURL.fileURLWithPath("/Users/robin/Programming/xcode/Projects/Quix/php_scripts/userRegister.php"))
            request.HTTPMethod = "POST"
            let postString = "email=\(userEmail)&password=\(userPassword)"
            request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
            print(postString)

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in

        if (error != nil) {
            print("error=\(error)")
        }

        do {
            if let parseJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {

                let resultValue = parseJSON["status"] as? String
                print("result: \(resultValue)")

                var isUserRegistered:Bool = false
                if (resultValue == "Success") { isUserRegistered = true }

                var messageToDisplay:String = parseJSON["message"] as! String!
                if (!isUserRegistered) {
                    messageToDisplay = parseJSON["message"] as! String!
                }

                dispatch_async(dispatch_get_main_queue(), {
                    let myAlert = UIAlertController(title: "Alert", message: messageToDisplay, preferredStyle: UIAlertControllerStyle.Alert)

                    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { action in
                        self.dismissViewControllerAnimated(true, completion: nil)
                    }

                    myAlert.addAction(okAction)
                    self.presentViewController(myAlert, animated: true, completion: nil)

                });


            }
        } catch let error as NSError {
            print(error.localizedDescription)
        }
    }
    task.resume()

In my database I have the table 'users' and the parameters id (auto_increment), email and password. I am using port: 3306 for mySQL aswell. The standard IP for MAMP is 127.0.0.1, should I use the IP: 127.0.0.1 or localhost:3306 aswell?

robinskafte
  • 61
  • 1
  • 10
  • Where do you get this error? Can you show the code and possibly the line? – Navid Mar 20 '16 at 18:46
  • Unrelated observation: If `userEmail` or `userPassword` are optionals, make sure to unwrap them. And to be safe, you really should be percent-escaping them, e.g. http://stackoverflow.com/a/25154803/1271826, because if the password included any reserved characters (e.g. `+` or `&`), they won't get sent properly unless you percent-escape the request. – Rob Mar 20 '16 at 19:12

1 Answers1

0

You are using a file URL with a NSURLSession request. You should use a https:// or http:// request. NSURLSession is for making network requests, not local file system requests. So, if you're running this on the iOS simulator, you can use http://localhost/.... But when you run it on an device, you'll have to supply it a host name that will resolve to your machine running MAMP.

By the way, if you use http, you may need to add a NSExceptionDomains entry as outlined in https://stackoverflow.com/a/31254874/1271826.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044