Getting Header Response in Swift

前端 未结 2 1558
猫巷女王i
猫巷女王i 2021-02-14 13:12

I am following this answer for making HTTP calls in my swift project. How to make an HTTP request in Swift?

and following is the code I am using to make a synchronous

2条回答
  •  粉色の甜心
    2021-02-14 14:11

    Try this:

     func getData(url: NSURL) {
        let config: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
        let session: NSURLSession = NSURLSession(configuration: config)
    
        let dataTask: NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: {(data: NSData!, urlResponse: NSURLResponse!, error: NSError!) -> Void in
    
            if let httpUrlResponse = urlResponse as? NSHTTPURLResponse
            {
                if error {
                    println("Error Occurred: \(error.localizedDescription)")
                } else {
                    println("\(httpUrlResponse.allHeaderFields)") // Error
                }
            }
            })
    
           dataTask.resume()
        }
    

提交回复
热议问题