I have been trying to implement the NSURLSessionDataDelegate, in particular to modify the cache policies that are in the HTTP Headers received in the response.
To do this I try to intercept the proposedResponse before it is cached by using the willCacheResponse method, but it does not get called.
I looked on this website and others but still I am not sure why the function willCacheResponse is not called.
Any ideas?
class ViewController: UIViewController, NSURLSessionDelegate, NSURLSessionDataDelegate, NSURLSessionTaskDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let requestUrl = "https://www.google.com/data.json"
let request = NSURLRequest(URL: NSURL(string: requestUrl)!)
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(
configuration: configuration,
delegate: self,
delegateQueue: NSOperationQueue.mainQueue()
)
let task = session.dataTaskWithRequest(request) {
data, response, error in
//print(data)
print(response)
//print(error)
}
task.resume()
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, willCacheResponse proposedResponse: NSCachedURLResponse, completionHandler: (NSCachedURLResponse?) -> Void) {
print("willCacheResponse")
completionHandler(proposedResponse)
}
}
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!!")
}
}