5

In an NSOperation subclass, I am using the following code to download an xml-file from our server, and then later parse it:

NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] 
                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                      timeoutInterval:15];
NSData * receivedData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

The second time I make the same request, the server returns a HTTP 304, and the cached response data is stored in receivedData. So far so good.

My question: is it possible to get this same cached response when the device is offline?

vtim
  • 101
  • 6
  • I assume you mean automatically from `NSURLConnection` and not just manually storing the data locally and coughing it up when the network is down. – NJones Jan 19 '12 at 14:55
  • Indeed. The data is cached anyway, it would be awesome if there was a way to load it when the network is down. – vtim Jan 19 '12 at 15:21

1 Answers1

0

NSURLCache does not support disk caching. You can simply store it manually or use this: https://github.com/steipete/SDURLCache

It is very simple cache that does its job...very simple usage, only one class...It supports etags also.



    // Do this only once in your app...
    SDURLCache *urlCache = [[SDURLCache alloc] initWithMemoryCapacity:1024*1024   // 1MB mem cache
                                                         diskCapacity:1024*1024*5 // 5MB disk cache
                                                             diskPath:[SDURLCache defaultCachePath]];
    [NSURLCache setSharedURLCache:urlCache];
    [urlCache release];


El Horrible
  • 161
  • 9
  • 1
    ios5 and ios6 do support disk caching. Would be nice to have some additional answers thrown in here about how to achieve this without SDURLCache... – Alfie Hanssen Mar 29 '13 at 14:28