Clearing UIWebview Cache

I used UIWebview to load a webpage using the loadRequest: method loadRequest: when I leave this scene, I call [self.webView stopLoading]; and free webView.

In the activity monitor, at the first start, I saw that the real memory increased by 4 MB, and with several starts / loading the real memory did not increase. It increases only once.

I checked webview account hold. This is correct, i.e. 0. I think UIWebView caches some data. How to avoid caching or deleting cached data? Or is there another reason for this?

+64
ios cocoa-touch uikit uiwebview
Mar 29 2018-11-11T00:
source share
9 answers

I really think that it can save cached information when you close UIWebView . I tried removing the UIWebView from my UIViewController by releasing it and then creating a new one. The new one remembered where I was when I returned to the address without restarting everything (he remembered that my previous UIWebView was registered).

So a few suggestions:

 [[NSURLCache sharedURLCache] removeCachedResponseForRequest:NSURLRequest]; 

This will remove the cached response for a particular request. There is also a call that will delete all cached responses for all requests running on the UIWebView :

 [[NSURLCache sharedURLCache] removeAllCachedResponses]; 

After that, you can try to delete all related cookies using UIWebView :

 for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) { if([[cookie domain] isEqualToString:someNSStringUrlDomain]) { [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie]; } } 

Swift 3:

 // Remove all cache URLCache.shared.removeAllCachedResponses() // Delete any associated cookies if let cookies = HTTPCookieStorage.shared.cookies { for cookie in cookies { HTTPCookieStorage.shared.deleteCookie(cookie) } } 
+113
Apr 09 2018-11-11T00:
source share

Do not disable caching completely, this will hurt the performance of your application, and this is not necessary. It is important that you explicitly configure the cache when the application starts and clear it if necessary.

So, in application:DidFinishLaunchingWithOptions: configure cache limits as follows:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { int cacheSizeMemory = 4*1024*1024; // 4MB int cacheSizeDisk = 32*1024*1024; // 32MB NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"]; [NSURLCache setSharedURLCache:sharedCache]; // ... other launching code } 

Once you set up the correct setting, then you need to clear the cache (for example, in applicationDidReceiveMemoryWarning or when closing the UIWebView ):

 [[NSURLCache sharedURLCache] removeAllCachedResponses]; 

and you will see that the memory is restored. I wrote about it here: http://twobitlabs.com/2012/01/ios-ipad-iphone-nsurlcache-uiwebview-memory-utilization/

+44
Jan 31 2018-12-12T00:
source share

You can disable caching by doing the following:

 NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil]; [NSURLCache setSharedURLCache:sharedCache]; [sharedCache release]; 
+9
Apr 25 2018-11-11T00:
source share

Swift 3.

 // Remove all cache URLCache.shared.removeAllCachedResponses() // Delete any associated cookies if let cookies = HTTPCookieStorage.shared.cookies { for cookie in cookies { HTTPCookieStorage.shared.deleteCookie(cookie) } } 
+5
Nov 23 '16 at 20:53
source share

My educated guess is that the memory usage that you see is not related to the contents of the page, but to the loading of the UIWebView and everything that supports the WebKit libraries. I like the UIWebView control, but it is a β€œheavy” control that pulls a very large block of code.

This code is a large subset of the iOS Safari browser and probably initializes a large number of static structures.

+2
Mar 31 '11 at 10:36
source share

After various attempts, this only works for me (under ios 8):

 NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:1 diskCapacity:1 diskPath:nil]; [NSURLCache setSharedURLCache:cache]; 
+1
Dec 13 '14 at 15:36
source share

For quick 2.0:

 let cacheSizeMemory = 4*1024*1024; // 4MB let cacheSizeDisk = 32*1024*1024; // 32MB let sharedCache = NSURLCache(memoryCapacity: cacheSizeMemory, diskCapacity: cacheSizeDisk, diskPath: "nsurlcache") NSURLCache.setSharedURLCache(sharedCache) 
+1
Jan 28 '16 at 15:42
source share

I could not change the code, so I need a command line for testing, and thought it might help someone:

The application cache is stored in ~/Library/Caches/<bundle-identifier-of-your-app> , so just delete it as shown below and reopen the application

rm -rf ~/Library/Caches/com.mycompany.appname/

+1
Mar 30 '16 at 22:06
source share

I am loading html pages from Documents and, if they have the same css name of the UIWebView file, it does not seem to abandon the previous CSS rules. Maybe because they have the same url or something like that.

I tried this:

 NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil]; [NSURLCache setSharedURLCache:sharedCache]; [sharedCache release]; 

I tried this:

 [[NSURLCache sharedURLCache] removeAllCachedResponses]; 

I load the start page:

 NSURLRequest *appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0]; 

He refuses to throw his cached data! Disruption!

I do this in the PhoneGap application (Cordova). I have not tried this in an isolated UIWebView.

Update1: I found this one .
Modifying html files, although it seems very dirty.

0
Nov 08 '12 at 17:34
source share



All Articles