21

I currently have a UIWebView that is displayed within a modal view. It is basically a detail view that provides a view of a page when the user clicks a link. When the view is dismissed and then brought up again (when the user clicks another link), the previously-loaded content is still visible and the new content loads "on top" of the last content. This makes sense because the instance of the UIWebView persists between sessions and is only released when the memory is needed.

However, I would like to completely clear the UIWebView when the modal view is dismissed so that 1) content is cleared and 2) memory is freed. Thus far my research and attempts have not found an answer. These links haven't worked for me:

  1. is it possible to free memory of UIWebView?
  2. Reused UIWebView showing previous loaded content for a brief second on iPhone

I've tried [[NSURLCache sharedURLCache] removeAllCachedResponses]; and setting the webView to nil and manually releasing the webView upon modal-view-dismiss to no avail. Any thoughts from the wizened masses?

Community
  • 1
  • 1
Ricky
  • 639
  • 2
  • 6
  • 12

6 Answers6

64

Sometimes loadHTMLString is too slow. You also can use:

[webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML = \"\";"];

I'm not sure if that will free up much memory though...

rob
  • 4,069
  • 3
  • 34
  • 41
  • 2
    Perfect! Probably the easiest solution for the visual clearing of the webview. Still don't understand why loadHTMLString is so slow. Maybe it's because I am loading another page directly afterwards, and webview stops loading and displaying the empty page when it starts to load the new one. – Cray Feb 22 '11 at 15:09
  • Best solution in here. Don't know why this is not the best voted answer. Anyway - Great answer!! – filou Feb 14 '13 at 00:26
  • 1
    The best thing about this method is that the delegate method webViewDidFinishLoad won't get called! – user523234 May 14 '13 at 14:50
  • BTW this would not clear up a pdf formatted content, any idea? – user523234 May 14 '13 at 14:58
  • Wow you solve my problem!!! javascript solution is 50% faster than the load nil string solution!... thank you so much! – Usi Usi Jul 17 '13 at 15:54
  • clearing the body without removing previously added events to objects will cause memory leak – Porizm Sep 25 '16 at 21:11
34

Releasing the web view is probably the best approach. If a view is in a view hierarchy (has a superview) you must call removeFromSuperview to get the superview to release the view.

You could also load an html string for an empty document:

[webView loadHTMLString:@"<html><head></head><body></body></html>" baseURL:nil];
drawnonward
  • 53,459
  • 16
  • 107
  • 112
14

It seems under some circumstances the methods suggested by drawnonward and rob may not work (possibly something to do with CSS on the page that's being cleared), however this did work :

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
Adrian B
  • 418
  • 3
  • 7
8

evaluating javascript is usually quicker than loading a new request.

[webview stringByEvaluatingJavaScriptFromString:@"document.open();document.close();"];
surajz
  • 3,471
  • 3
  • 32
  • 38
5

In my case I needed to clear the web view from its contents instantaneously and be able to start loading something else right away. Neither loading an empty document HTML string nor clearing the page with JavaScript resulted in desired effect. Instead I used:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://stackoverflow.com"]]];

And this cleared the view to the default background and initiated next page's loading as expected.

I hope this helps someone else as well.

Ivan Karpan
  • 1,554
  • 15
  • 20
0

i prefer to the following:

[m_webView stringByEvaluatingJavaScriptFromString:@"location.replace('about:blank')"]
zqxiaojin
  • 69
  • 5