在iOS 9中不建议使用sendSynchronousRequest [英] sendSynchronousRequest is deprecated in ios 9

查看:215
本文介绍了在iOS 9中不建议使用sendSynchronousRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Xcode表示sendSynchronousRequest现在已弃用.

Xcode says that sendSynchronousRequest is now deprecated.

我应该如何更换它?

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()
}

推荐答案

这是一个有效的示例, 您应该将NSURLSession与Request一起使用.

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()
        }

注意,没有必要使用该请求.您可以使用URL进行数据任务,但是我添加了请求,因为在您的代码中,您已经在请求中设置了一些标头.

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.

使用completionHandler的通知,当服务器通过http响应进行响应时会调用completionHandler.

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

这篇关于在iOS 9中不建议使用sendSynchronousRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆