I was able to manually cache pages by writing them to the sharedURLCache like this:
Alamofire.request(req)
.response {(request, res, data, error) in
let cachedURLResponse = NSCachedURLResponse(response: res!, data: (data as NSData), userInfo: nil, storagePolicy: .Allowed)
NSURLCache.sharedURLCache().storeCachedResponse(cachedURLResponse, forRequest: request)
}
NSURLCache seems to respect the headers sent by the server, even if you configure the opposite everywhere else in your code.
The Wikipedia API, for example, sends
Cache-control: private, must-revalidate, max-age=0
Which translates to: Must revalidate after 0 seconds.
So NSURLCache says: “OK, I won’t cache anything.”
But by manually saving the response to the cache, it works. At least on iOS 8.2.
Almost lost my mind on this one. :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…