Search code examples
ioscachinguiwebviewafnetworkingnsurlcache

Saving/Caching files using AFNetworking and using it for online and offline access


Our app is serving content in UIWebView which can link to PDF files, Video files, Doc files etc...We also allow user to access all of this content (Web and the linked files) in offline mode by downloading all the required files. Once all the files are available locally, we'd like the web view to use these local files even when the user is online.

  1. If we setup big enough NSURLCache and use AFNetworking to download the files and save it on disk will this work out of the box?
  2. Is there a way to put the downloaded content (files) in cache and then load the content in web view so that it uses the cached files?

Solution

  • You can achieve this by subclassing the AFHTTPSessionManager. Here you can check if the client is offline. And then you can change the cache policy or force the app to use cached data.

    if (![[AFNetworkReachabilityManager sharedManager] isReachable]) {
        [request setCachePolicy:NSURLRequestReturnCacheDataElseLoad];
    }