How the iphone empties the UIWebView cache

Source: Internet
Author: User

Iphonecachingapplicationcookiescacheperformance

I actually think it may retain cached information when you close out the 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 am at while I went back to an address without have to reload everything (it remem Bered my previous UIWebView is 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 would remove all cached responses for all requests ran on the UIWebView:

[[NSURLCache sharedURLCache] removeAllCachedResponses];

After this, you can try deleting any associated cookie with the UIWebView:

For(Nshttpcookie *Cookie in[[Nshttpcookiestorage Sharedhttpcookiestorage] Cookies)  {    if ([[cookie domain] Isequaltostring: somensstringurldomain)  {        [[nshttpcookiestorage Sharedhttpcookiestorage]:cookie];}}        

Let me know where this gets you.

I had nearly the same problem. I wanted the webview cache to being cleared, because everytime i reload a local webpage in an UIWebView, the old one is shown . So I found a solution by simply setting thecachepolicy property of the request. Use a nsmutableurlrequest to set the This property. With all this everything works fine with reloading the UIWebView.

Nsurl*Url= [Nsurl Fileurlwithpath:myhtmlfilepath];  nsmutableurlrequest *Request = [nsmutableurlrequest requestwithurl: URL];  [request Setcachepolicy:nsurlrequestreloadignoringlocalcachedata];  [self.  WebView loadrequest:request];            

Hope that helps!

Don ' t disable caching completely, it ' ll hurt your app performance and it ' s unnecessary. The important thing is to explicitly configure the cache in app startup and purge it when necessary.

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

- (BOOL)Application:(UIApplication *)Application Didfinishlaunchingwithoptions:(Nsdictionary *)Launchoptions{IntCachesizememory= 4*1024*1024; 4MBIntCachesizedisk= 32*1024*1024; 32MBNsurlcache *sharedcache = [[[nsurlcache Alloc]:cachesizememory diskcapacity:cachesizedisk diskpath:@ "Nsurlcache"   Autorelease;     [nsurlcache:sharedcache];//... other launching Code}    

Once you have it properly configured and then if you need to purge the cache (for example in applicationDidReceiveMemoryWarning or if 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/

You can disable the caching by doing the following:

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

How the iphone empties the UIWebView cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.