0

Here's my edited question:

How can I limit cache size from WebView on iOS app?

Can I use one of these codes?

[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:X];
[[NSURLCache sharedURLCache] setMemoryCapacity:X];

Or is there a possibility to clear cache by clicking a button?

  • There are various posts that describe how you can remove UIWebView's chache. Such as [this](http://stackoverflow.com/questions/15054472/how-clear-uiwebview-cache) or [this](http://stackoverflow.com/questions/2523435/how-to-clear-uiwebview-cache) or [this](http://stackoverflow.com/questions/5468553/clearing-uiwebview-cache) – UditS Mar 04 '16 at 19:06
  • Your edited question is completely different than the original one and this makes Hugo's answer out of context. Please roll-back the question and consider asking this as a different / new question. – UditS Mar 06 '16 at 00:32

1 Answers1

0

The easiest way to clear UIWebview cache is:

Objective-C

[[NSURLCache sharedURLCache] removeAllCachedResponses];

Swift

NSURLCache.sharedURLCache().removeAllCachedResponses()

Now, for your case, you may want to use this function after calculating proper date:

Clears the given cache of any cached responses since the provided date.
     */
    @available(iOS 8.0, *)
    public func removeCachedResponsesSinceDate(date: NSDate)

A good scenario would be:

1.- Store the last time/date you cleaned cache (or first time the app is used) in NSUserDefaults.

2.- Every time you enter your app (or in background fetch) calculate if has being 24h or more since that date

3.- If true, then clear cache and return to step 1

Hugo Alonso
  • 6,684
  • 2
  • 34
  • 65
  • Hi, thanks for trying to help me. But I'm a noob at this so I'm not able to do this. Do you have a sample code for me? –  Mar 04 '16 at 21:30
  • There are a lot of matters here to take into account. Try to study a little about these topics and back to me again. It would be very large to post here. – Hugo Alonso Mar 04 '16 at 21:53
  • Okay, I'll do it. I've a small question: What do I have to use to limit the cache to e.g. five websites? [[NSURLCache sharedURLCache] setDiskCapacity:X]; or [[NSURLCache sharedURLCache] setMemoryCapacity:X]; Will X be the number of websites or data in MB? –  Mar 04 '16 at 22:01