Eloy Durán edited this page Mar 6, 2015 · 2 revisions
Clone this wiki locally

How does AFCache achieve caching requests from UIWebViews?

via AFURLCache which is a subclass of NSURLCache.

Correct me if I'm wrong, but it appears that you have to use the AFCache API to actually cache/store a cached response.

You don't have to. You can use AFCache as a transparent cache. Just register the cache via

    AFURLCache* urlCache = [[[AFURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:@""] autorelease];
    [NSURLCache setSharedURLCache:urlCache];    

NSURLRequests will be cached then. See NSURLRequestDemoViewController.m, I have just updated the master branch on github. This demo requests an image from wikipedia, and uses a classic way by creating an NSURLRequest and storing the result. As you'll see, the image that will be displayed displayed is inverted. This is because the image is already on disk (with an expire date in the future). Because the appdelegate has registered the cache, all NSURLRequests will be delegated to AFCache.

What about the AFCache API? Why do I would use it?

The AFCache API has some convenience methods that care about receiving data, parsing headers, storing data, error handling, etc. See CacheableItemDemoController.m for basic usage.

I guess I'm confused as to what AFCache actually does.

Basically it stores NSURLResponses on disk and serves them from there on subsequent requests. AFCache checks for freshness of the file. If the cached file on disk is outdated, AFCache transparently updates it and gives you the new version. I wrote AFCache because on iPhone, NSURLCache doesn't write to disk. In the last months AFCache has evolved to something like a cache package management tool. You may package a bunch of files on the server (via the afcpkg commandline tool), request this package from your client and prefill the cache instead of doing many requests for small files. One uses case would be a digital magazine issue downloaded into the cache (e.g. for offline reading).

If I requested the URL "http://www.artifacts.de/" in a UIWebView, is it possible for AFCache to cache the responses from the implicit requests made from within the UIWebView?

Yes. All implicit requests done by the UIWebView will be dispatched to NSURLCache (or a subclass of it), if registered. See PackagingDemoController.m, which displays an UIWebView (wikipedia inverted again). The cache has been prefilled before via a package (hosted at "http://www.artifacts.de/afcache/demopackage.zip"), then taken into offline mode. If you would examine the package you'll discover the inverted wikipedia logo and a manifest file containing expiry information. Note: you don't have to use these packages by any means, but they might be handy if you want to transfer a bunch of files to the client for offline access. If you just want AFCache to store your files, register it like described above at everything should work.