I have used UIWebview to load a web page using loadRequest:
method, when I leave that scene I call [self.webView stopLoading];
and release the webView.
In activity monitor on first launch i have seen that the real memory increased by 4MB, and on multiple launches/loading the real memory doesn't increase. It is increasing only once.
I have checked the retain count of webview. It is proper i.e., 0. I think UIWebView is caching some data. How do I avoid caching or remove cached data? Or is there another reason for this?
How do I clear cache in iOS WebView? Open Settings. Swipe down and tap Safari. Swipe down again and tap Clear History and Website Data, tap it once again to confirm.
How do I clear cache in react-native WebView? For android, the “App info” setting, under Storage will allow you to clear cache. This is how you can force a reload of any resources you are displaying in the WebView.
I actually think it may retain cached information when you close out the UIWebView
. I've tried removing a UIWebView
from my UIViewController
, releasing it, then creating a new one. The new one remembered exactly where I was at when I went back to an address without having to reload everything (it remembered my previous UIWebView
was logged in).
So a couple of suggestions:
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:NSURLRequest];
This would remove a cached response for a specific request. There is also a call that will remove all cached responses for all requests ran on the UIWebView
:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
After that, you can try deleting any associated cookies with the 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) } }
Don't disable caching completely, it'll hurt your app performance and it's unnecessary. The important thing is to explicitly configure the cache at app startup and purge it when necessary.
So in application:DidFinishLaunchingWithOptions:
configure the 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 have it properly configured, then when you need to purge the cache (for example in applicationDidReceiveMemoryWarning
or when you close a UIWebView
) just do:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
and you'll see the memory is recovered. I blogged about this issue here: http://twobitlabs.com/2012/01/ios-ipad-iphone-nsurlcache-uiwebview-memory-utilization/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With