Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use NSURLCache to return cached API responses when offline (iOS App)
    primarykey
    data
    text
    <p>I'm hoping someone can shed some light on a few things I've been researching but not making much progress on.</p> <p>I'd like to take advantage of NSURLCache to return cached responses for API calls that I make within an iOS App. When the device is online, I'd like to return the cached response if it's recent enough, otherwise fetch from remote. When the device is offline I'd like to immediately return the cached response (if any) regardless of it's age. </p> <p>I'm using <code>AFNetworking</code>. The API calls that I'm making are to a server I control. Protocol is HTTPS. The <code>Cache-Control</code> response header is currently <code>"max-age=0, public"</code>. I'm not currently setting cache related request headers (should I?). I set the request's cache policy to be <code>NSURLRequestReturnCacheDataDontLoad</code> when offline and use the default <code>NSURLRequestUseProtocolCachePolicy</code> when online. And I can see the requests and their responses in the default <code>Cache.db</code> on disk. However when I'm offline all requests fail (no cached responses (despite appearing to have been cached) are being used/returned. </p> <p>Per <a href="http://nshipster.com/nsurlcache/">http://nshipster.com/nsurlcache/</a> I initialize a <code>sharedURLCache</code> in <code>didFinishLaunchingWithOptions</code> and set the AFNetworking <code>setCacheResponse</code> block to something like this:</p> <pre> NSMutableDictionary *mutableUserInfo = [[cachedResponse userInfo] mutableCopy]; NSMutableData *mutableData = [[cachedResponse data] mutableCopy]; NSURLCacheStoragePolicy storagePolicy = NSURLCacheStorageAllowed; return [[NSCachedURLResponse alloc] initWithResponse:[cachedResponse response] data:mutableData userInfo:mutableUserInfo storagePolicy:storagePolicy]; </pre> <p>I've read these and other posts on the topic:</p> <p><a href="http://petersteinberger.com/blog/2012/nsurlcache-uses-a-disk-cache-as-of-ios5/">http://petersteinberger.com/blog/2012/nsurlcache-uses-a-disk-cache-as-of-ios5/</a> <a href="http://blackpixel.com/blog/2012/05/caching-and-nsurlconnection.html">http://blackpixel.com/blog/2012/05/caching-and-nsurlconnection.html</a></p> <p>I'm wondering if anyone out there has successfully achieved this functionality before using the standard <code>NSURLCache</code> (also interested in success stories involving <code>SDURLCache</code> but Peter S. says as of iOS5 disk caching is supported therefore <code>SDURLCache</code> is no longer needed, ie I'd like to use the default built in cache). </p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload