Josh R
Josh R

Reputation: 29

Updating an URLSession to swift 3 throwing error

I updated my code to Swift 3 and most of it converted over fine except from the URLSession and I cant find a solution to this error:

Cannot invoke 'dataTask' with an argument list of type '(with: NSMutableURLRequest, completionHandler: (Data?, URLResponse?, NSError?) -> Void)'

This is my code:

    let post:NSString = "username=\(username)&userPassword=\(password)&userEmail=\(email)" as NSString

    let url:URL = URL(string: "http://ec2-54-201-55-114.us-west-2.compute.amazonaws.com/wickizerApp/ApplicationDB/scripts/registerUser.php")!

    let request = NSMutableURLRequest(url: url)
    request.httpMethod = "POST"
    request.httpBody = post.data(using: String.Encoding.utf8.rawValue)


    URLSession.shared.dataTask(with: request, completionHandler: { (data:Data?, response:URLResponse?, error:NSError?) -> Void in

        DispatchQueue.main.async
            {

                if error != nil {
                    self.displayAlertMessage(error!.localizedDescription)
                    return
                }

                do {
                    let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

                    if let parseJSON = json {

                        let status = parseJSON["status"] as? String

                        if( status! == "200")
                        {
                            let myAlert = UIAlertController(title: "Alert", message: "Registration successful", preferredStyle: UIAlertControllerStyle.alert);

                            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default){(action) in

                                self.dismiss(animated: true, completion: nil)
                            }

                            myAlert.addAction(okAction);
                            self.present(myAlert, animated: true, completion: nil)
                        } else {
                            let errorMessage = parseJSON["message"] as? String
                            if(errorMessage != nil)
                            {
                                self.displayAlertMessage(errorMessage!)
                            }

                        }

                    }
                } catch{
                    print(error)
                }



        }

    }).resume()

Is there a different way to do requests in swift 3 or did they just change the way to do them?

Upvotes: 0

Views: 600

Answers (1)

vadian
vadian

Reputation: 285270

The compiler wants URLRequest and Error

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = post.data(using: .utf8)

URLSession.shared.dataTask(with: request, completionHandler: { (data:Data?, response:URLResponse?, error:Error?) -> Void in

})

or still shorter

URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in

})

or still shorter

URLSession.shared.dataTask(with: request) { (data, response, error) in

}

Upvotes: 2

Related Questions