How to delete the cache from UIWebview or dealloc UIWebview
Solution 1
My solution to this problem was to create the UIWebView
programmatically on viewWillAppear:
and to release it on viewDidDisappear:
, like this:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.webView = [[[UIWebView alloc]initWithFrame:exampleFrame] autorelease];
self.webView.scalesPageToFit = YES;
self.webView.delegate = self;
self.webView.autoresizingMask = webViewBed.autoresizingMask;
[exampleView addSubview:self.webView];
}
- (void) viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self.webView removeFromSuperview];
self.webView.delegate = nil;
self.webView = nil;
}
If you do this and your UIWebView
doesn't get released you should check it's retain count and understand who is retaining it.
Solution 2
I had nearly the same problem. I wanted the webview cache to be cleared, because everytime i reload a local webpage in an UIWebView
, the old one is shown. So I found a solution by simply setting the cachePolicy
property of the request. Use a NSMutableURLRequest
to set this property. With all that 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!
Solution 3
If you want to remove all cached responses, you may also want to try something like this:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
Solution 4
I did something simpler, I just set the webView's alpha
to 0 on loadRequest and 1 on webViewDidFinishLoad.
thewebview.alpha=0.0;
thewebview.backgroundColor=[UIColor whiteColor];
[thewebview loadRequest:[NSURLRequest requestWithURL:url(str)]];
and
- (void)webViewDidFinishLoad:(UIWebView *)webView {
thewebview.alpha=1.0;
}
(Actually I have a fade-in, as rendering f.ex. a PDF can take 1/5 seconds or so.)
Solution 5
If I had to make a guess your property for webView is
@property (nonatomic, retain) IBOutlet UIWebView *webView;
Correct? The important part is the retain.
self.webView = [[UIWebView alloc]initWithFrame:frame];
The above code allocates a UIWebView (meaning retainCount = 1) and then adds it to self.webView, where it is retained again (retainCount = 2).
Change the code to the following, and your webView should be deallocated.
self.webView = [[[UIWebView alloc]initWithFrame:frame] autorelease];
addSubview: can only be called on UIViews.
//[self.navigationController addSubview:self.webView];
should be
[self.navigationController.view addSubview:self.webView];
testing
Updated on April 05, 2020Comments
-
testing about 4 years
Every time I load a new page with
UIWebView
the before loaded page is shown for a short time.How can I clear that cache? Another possibility would be to dealloc
UIWebview
. I tried that but than myUIWebView
is always "empty". How should thealloc
anddealloc
be done in this case?I noticed that the
UIWebView
is consuming about 10 MB RAM. Now theUIWebView
is loaded together with theViewController
. And the view is autoreleased as well as theUIWebView
is autoreleased. Wouldn't it be better to dealloc the WebView each time?Solution:
- (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; CGRect frame = CGRectMake(0, 0, 320, 480); self.webView = [[[UIWebView alloc]initWithFrame:frame] autorelease]; self.webView.scalesPageToFit = YES; [self.view addSubview:self.webView]; } - (void) viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [self.webView removeFromSuperview]; self.webView = nil; }
-
testing over 13 yearsWhat is exampleFrame, webViewBed and exampleView? What is autoresizingMask? I don't implement the
UIWebViewDelegate
protocol so I don't need the delegate.addSubview
don't work for me, because myUIViewController
does not support this. As far as I can see you are not releasing the web view onviewDidDisappear
. You only remove it from the superview and set it tonil
. I tried your approach and I only can see a white screen. I edited my question so you can see my code. -
testing over 13 yearsYou're right. But I don't understand that why the retain count of 2 causes the webview to display a white screen. Thank you for your help. Whom should I give the check mark? It's difficult ...
-
Matthias Bauch over 13 yearsI don't know what or even if the white screen has something to do with this issue. But if you have memory management issues left it's complicated to debug other problems. So first fix the memory management and investigate further into the white screen issue.
-
mips over 13 yearsexampleFrame is just the size of the UIWebView you are creating. webViewBed shouldn't be a part of this example, it's not useful in this context. exampleView is the view you're adding the UIWebView to. And of course, you addSubview()s to Views, not to view controllers. So you'll have to do something like [viewController.view addSubview:...]. I am releasing the webview, it's a property (see the "self."?) so you can set it to nil and it'll be released.
-
markshiz over 12 yearsthis did not solve my problem for whatever reason. i'm guessing it's a bug in the API.
-
user621267 almost 12 yearsThanks: "removeAllCachedResponses" did not work for me but this did.
-
Bagusflyer almost 12 yearsWhat about if I'm using loadHtmlString? How can I clear the cache every time I loadHtmlString?
-
Juan T almost 11 yearsOnly with [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; and [_webView loadRequest:request]; my UIWebView did not reload the page. I had to add [_webView reload];
-
Ravikant over 7 yearsPerfect!! This is the only solution on the internet that i found to be working!!