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

后端 未结 10 2192
长发绾君心
长发绾君心 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:19

    Here is the SWIFT3.0 Version of Nilesh Patel's Answer with JSONSerialised data

    let url = URL(string: "")!
                var request = URLRequest(url: url)
                request.httpMethod = "POST" //GET OR DELETE etc....
                request.setValue("application/json", forHTTPHeaderField: "Content-Type")
                request.setValue("", forHTTPHeaderField: "Authorization")
                let parameter = [String:Any]() //This is your parameters [String:Any]
                do {
                    let jsonData = try JSONSerialization.data(withJSONObject: parameter, options: .prettyPrinted)
                    // here "jsonData" is the dictionary encoded in JSON data
                    request.httpBody = jsonData
                    let session = URLSession(configuration: .default)
                    let task = session.dataTask(with: request, completionHandler: { (incomingData, response, error) in
                        if let error = error {
                            print(error.localizedDescription)
                            print(request)
                        }else if let response = response {
                            print(response)
                        }else if let incomingData = incomingData {
                            print(incomingData)
                        }
                    })
                    task.resume()
    
                } catch {
                    print(error.localizedDescription)
                }
    

提交回复
热议问题