sendSynchronousRequest is deprecated in ios 9 [duplicate]

蓝咒 提交于 2019-12-13 11:25:19

问题


Xcode says that sendSynchronousRequest is now deprecated.

How should I replace it?

let postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)!
let postLength:NSString = String( postData.length )
let request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody = postData
request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")

var response: NSURLResponse?
var urlData: NSData?
do {
    urlData = try NSURLConnection.sendSynchronousRequest(request, returningResponse:&response)
} catch _ as NSError {
    urlData = nil
} catch {
    fatalError()
}

回答1:


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.



来源:https://stackoverflow.com/questions/32951490/sendsynchronousrequest-is-deprecated-in-ios-9

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!