NSURLSessionDataDelegate not called

Solution 1:

class ViewController: UIViewController, NSURLSessionDelegate, NSURLSessionDataDelegate, NSURLSessionTaskDelegate {
    var session:NSURLSession!

    override func viewDidLoad() {
        super.viewDidLoad()

        let requestUrl = "https://www.google.com"
        let request = NSURLRequest(URL: NSURL(string: requestUrl)!)
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()

        self.session = NSURLSession(
            configuration: configuration,
            delegate: self,
            delegateQueue: NSOperationQueue.mainQueue()
        )

        let task = session.dataTaskWithRequest(request)


        task.resume()
    }

    func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
        print("Did receive data!")
    }

    func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
        print("Response!!")
    }
}

Solution 2:

Try:

func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
    completionHandler(.Allow)
}

This worked for me and now the other delegate methods gets called.

Solution 3:

Not every responses are cached: "Invoke the completion routine with a valid NSCachedURLResponse to * allow the resulting data to be cached, or pass nil to prevent * caching. Note that there is no guarantee that caching will be * attempted for a given resource, and you should not rely on this * message to receive the resource data."

Try to see if the other methods of the delegate are called.