Clear UIWebView content upon dismissal of modal view (iPhone OS 3.0)

后端 未结 6 717
醉梦人生
醉梦人生 2020-12-08 11:12

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

相关标签:
6条回答
  • 2020-12-08 11:17

    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"]]];
    
    0 讨论(0)
  • 2020-12-08 11:21

    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...

    0 讨论(0)
  • 2020-12-08 11:23

    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];
    
    0 讨论(0)
  • 2020-12-08 11:24

    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.

    0 讨论(0)
  • 2020-12-08 11:29

    evaluating javascript is usually quicker than loading a new request.

    [webview stringByEvaluatingJavaScriptFromString:@"document.open();document.close();"];
    
    0 讨论(0)
  • 2020-12-08 11:36

    i prefer to the following:

    [m_webView stringByEvaluatingJavaScriptFromString:@"location.replace('about:blank')"]
    
    0 讨论(0)
提交回复
热议问题