Bypassing http response header Cache-Control: how to set cache expiration?

后端 未结 3 715
面向向阳花
面向向阳花 2020-12-25 08:56

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
         


        
3条回答
  •  被撕碎了的回忆
    2020-12-25 09:27

    Another possible solution is to modify the response object and clobber the Cache-Control headers from the server and replace them with your own desired values.

    There are two places you could do that.

    You could do it in a NSURLSessionDataDelegate in func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, willCacheResponse proposedResponse: NSCachedURLResponse, completionHandler: (NSCachedURLResponse?) -> Void), but if you do it there, then can no longer use the usual completion handler-based methods getting results from session tasks.

    Another place you could do it is by defining a custom NSURLProtocol, which intercepts HTTP and HTTPS responses and modifies them.

提交回复
热议问题