[Solved] sendSynchronousRequest is deprecated in ios 9 [duplicate]


This is a working example,
You should use NSURLSession, with Request.

     func testPost(sender: UIButton) {
            let session = NSURLSession.sharedSession()
            let request = NSMutableURLRequest(URL: NSURL(string: "http://localhost:8080/iOSServer/ios/helloworld/swiftCalculator")!)
            request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
            request.HTTPMethod = "POST"
            let d = "4"
            let data = "x=4&y=\(d)"
            request.HTTPBody = data.dataUsingEncoding(NSASCIIStringEncoding)
            let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
                if let error = error {
                    print(error)
                }
                if let data = data{
                    print("data =\(data)")
                }
                if let response = response {
                print("url = \(response.URL!)")
                print("response = \(response)")
                let httpResponse = response as! NSHTTPURLResponse
                print("response code = \(httpResponse.statusCode)")

                //if you response is json do the following
                  do{
                    let resultJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())
                    let arrayJSON = resultJSON as! NSArray
                    for value in arrayJSON{
                        let dicValue = value as! NSDictionary
                        for (key, value) in dicValue {
                            print("key = \(key)")
                            print("value = \(value)")
                        }
                    }

                }catch _{
                    print("Received not-well-formatted JSON")
                }
            }
            })
            task.resume()
        }

Notice it is not necessary to use the request. you can have a data task with URL, but I added the request because in your code, you have set some headers in the request.

Notice using the completionHandler which will be called when your server responses by http response.

8

solved sendSynchronousRequest is deprecated in ios 9 [duplicate]