sendAsynchronousRequest was deprecated in iOS 9, How to alter code to fix

后端 未结 10 2173
长发绾君心
长发绾君心 2020-12-04 15:48

Below is my code I am getting the issue with:

func parseFeedForRequest(request: NSURLRequest, callback: (feed: RSSFeed?, error: NSError?) -> Void)
{
    N         


        
10条回答
  •  春和景丽
    2020-12-04 16:18

    Use NSURLSession instead like below,

    For Objective-C

    NSURLSession *session = [NSURLSession sharedSession];
    [[session dataTaskWithURL:[NSURL URLWithString:"YOUR URL"]
              completionHandler:^(NSData *data,
                                  NSURLResponse *response,
                                  NSError *error) {
                // handle response
    
      }] resume];
    

    For Swift,

        var request = NSMutableURLRequest(URL: NSURL(string: "YOUR URL")!)
        var session = NSURLSession.sharedSession()
        request.HTTPMethod = "POST"
    
        var params = ["username":"username", "password":"password"] as Dictionary
    
        request.HTTPBody = try? NSJSONSerialization.dataWithJSONObject(params, options: [])
    
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue("application/json", forHTTPHeaderField: "Accept")
    
        var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
            print("Response: \(response)")})
    
        task.resume()
    

    For asynchronously query, from Apple docs

    Like most networking APIs, the NSURLSession API is highly asynchronous. It returns data in one of two ways, depending on the methods you call:

    To a completion handler block that returns data to your app when a transfer finishes successfully or with an error.

    By calling methods on your custom delegate as the data is received.

    By calling methods on your custom delegate when download to a file is complete.

提交回复
热议问题