Centurion
Centurion

Reputation: 14304

How to set cache expiration time when using UIWebView and NSURLRequest?

I'm looking for the most simple default solution. Currently, I have one about view controller where some about info from some url is shown. I need to cache that for offline usage, and cache should be updated after some time, for example after a week. Currently , I'm using NSURLRequestReturnCacheDataElseLoad cache policy but don't know how to set cache expiration and cache update time:

- (void)viewDidLoad
{
  [super viewDidLoad];
   // Do any additional setup after loading the view from its nib.

   NSString *urlString = [NSString stringWithFormat:@"%@%@", kServiceBaseUrl, @"docs/about_en.html"];

   NSURL *url = [NSURL URLWithString:urlString];
   NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];

   [self.webView loadRequest:request];
}

I have read several posts where manual cache handling (get NSData, save, check and load) is suggested. But maybe there's more straightforward and simple solution?

Upvotes: 0

Views: 1771

Answers (1)

sangony
sangony

Reputation: 11696

You can manually clear the cache like this:

[[NSURLCache sharedURLCache] removeAllCachedResponses];

As for expiration and handling options, take a look at the NSURLRequest Class Reference dealing with cache here.

You can get a detailed explanation of Understanding Cache Access from the Apple URL Loading System Programming Guide here.

Upvotes: 1

Related Questions