Created
December 4, 2014 07:13
-
-
Save Gurpartap/1833e282b07a503da751 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, willCacheResponse proposedResponse: NSCachedURLResponse, completionHandler: (NSCachedURLResponse!) -> Void) { | |
let response = proposedResponse.response | |
let httpResponse = response as NSHTTPURLResponse | |
let headers: [NSObject: AnyObject] = httpResponse.allHeaderFields | |
var cachedResponse: NSCachedURLResponse! | |
if !contains(headers.keys, "Cache-Control") { | |
var modifiedHeaders: [NSObject: AnyObject] = headers | |
modifiedHeaders["Cache-Control"] = "max-age=60" | |
let modifiedResponse = NSHTTPURLResponse(URL: httpResponse.URL!, statusCode: httpResponse.statusCode, HTTPVersion: "HTTP/1.1", headerFields: modifiedHeaders) | |
cachedResponse = NSCachedURLResponse(response: modifiedResponse!, data: proposedResponse.data, userInfo: proposedResponse.userInfo, storagePolicy: proposedResponse.storagePolicy) | |
} else { | |
cachedResponse = proposedResponse | |
} | |
completionHandler(cachedResponse) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment