All http responses from a server come with the headers that inform our app not to cache the responses:
Cache-Control: no-cache
Pragma: no-cache
Expires: 0
>
If somebody would be interested, here is Stephanus answer rewritten in Swift:
class CustomURLCache: NSURLCache {
// UserInfo expires key
let kUrlCacheExpiresKey = "CacheData";
// How long is cache data valid in seconds
let kCacheExpireInterval:NSTimeInterval = 60*60*24*5;
// get cache response for a request
override func cachedResponseForRequest(request:NSURLRequest) -> NSCachedURLResponse? {
// create empty response
var response:NSCachedURLResponse? = nil
// try to get cache response
if let cachedResponse = super.cachedResponseForRequest(request) {
// try to get userInfo
if let userInfo = cachedResponse.userInfo {
// get cache date
if let cacheDate = userInfo[kUrlCacheExpiresKey] as NSDate? {
// check if the cache data are expired
if (cacheDate.timeIntervalSinceNow < -kCacheExpireInterval) {
// remove old cache request
self.removeCachedResponseForRequest(request);
} else {
// the cache request is still valid
response = cachedResponse
}
}
}
}
return response;
}
// store cached response
override func storeCachedResponse(cachedResponse: NSCachedURLResponse, forRequest: NSURLRequest) {
// create userInfo dictionary
var userInfo = NSMutableDictionary()
if let cachedUserInfo = cachedResponse.userInfo {
userInfo = NSMutableDictionary(dictionary:cachedUserInfo)
}
// add current date to the UserInfo
userInfo[kUrlCacheExpiresKey] = NSDate()
// create new cached response
let newCachedResponse = NSCachedURLResponse(response:cachedResponse.response, data:cachedResponse.data, userInfo:userInfo,storagePolicy:cachedResponse.storagePolicy)
super.storeCachedResponse(newCachedResponse, forRequest:forRequest)
}
}