25

I have a webview that can be cycled through different URLs. When I switch from one to the other I want the old web page to disappear before loading the next one. How can I do this without re allocating the webview?

If I try to [self.webView loadHTMLString:@"" baseURL:nil]; and load my URL in the same function the previous web page still remains:

[self.webView loadHTMLString:@"" baseURL:nil];
[self.webView loadRequest:[NSURLRequest requestWithURL:self.pageURL]];

EDIT: this apparently isn't clear enough for you. the below code doesn't clear the webview, it displays the previous page until the new one is loaded:

- (void) startLoadOfNextURL:(NSURL*)url
{
    // clear:
    [self.webView loadHTMLString:@"" baseURL:nil]; //DOESNT WORK  

    // Load real next URL
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}
Halpo
  • 2,714
  • 3
  • 23
  • 51
  • 2
    can you pl. refer to the below link which discusses how to clear the webview http://stackoverflow.com/questions/6576154/how-to-clear-a-uiwebview – shri Oct 20 '15 at 10:22
  • 1
    it doesn't answer my question – Halpo Oct 20 '15 at 10:29
  • you need to clear the old webpage and load the new one right, i have tried the below approach mentioned an the link and it has helped me Try setting the URL to about:blank and reload the page. – shri Oct 20 '15 at 10:46
  • read my question again - I want to clear the page and immediately load a webpage i.e (webpage -> click -> blank -> new page) – Halpo Oct 20 '15 at 10:55
  • Can't you just hide the webview when you "click" and show it again when the delegate method reports that it's loaded? – haluzak Nov 14 '15 at 15:26
  • @Halpo Did you find a solution to this problem. I am having a similar issue when dequeuing a UITableViewCell that contains a WKWebView. While the cell's url is loaded, the data (page) of the "dequeued" cell is still displayed. I am thinking of removing the Webview and adding a new one at this stage. (Seeing that the UITableViewCell contain more controls than just the webview.) – Carien van Zyl Oct 27 '16 at 13:51
  • 1
    @CarienvanZyl I think the only reliable way is to re-init the web view – Halpo Oct 28 '16 at 07:50

6 Answers6

25

You can write below code while your controller dismiss.

webView.load(URLRequest(url: URL(string:"about:blank")!))
Dongjin Suh
  • 448
  • 4
  • 12
Donal
  • 5,222
  • 2
  • 18
  • 29
11

You can make it load a blank page

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
ogres
  • 3,642
  • 1
  • 19
  • 16
  • I know, but I want to clear the webview then immediately load a new page – Halpo Oct 20 '15 at 10:30
  • This answer is the key to your problem. Just load `about:blank` to *clear* the page and then load whatever you need to. – Soberman Oct 20 '15 at 11:40
  • 5
    but I cant do both in the same function (the page doesnt clear, please just read my question) – Halpo Oct 20 '15 at 15:47
7

The following code clears the screen and then navigates

Swift

webView.evaluateJavaScript("document.documentElement.remove()") { (_, _) in
    self.webView.load(urlRequest)
}
sibur
  • 71
  • 1
  • 2
6

Use JavaScript instead.

webView.evaluateJavaScript("document.body.remove()")
yesleon
  • 828
  • 11
  • 12
5

To clear old contents of webview

When you call - loadHTMLString:baseURL: it doesn't block until the load is complete. Once it records your request, it returns and loads in the background.

As a result, you would need to wait for the first load to finish before kicking off a new load request.

With UIWebView you would use UIWebViewDelegate's - webViewDidFinishLoad:.

With WKWebView you would use WKNavigationDelegate's - webView:didFinishNavigation:

Another approach if you really wanted to clear the contents without a delegate method would be to use JavaScript (eg https://stackoverflow.com/a/4241420/3352624). Then for UIWebView you could invoke - stringByEvaluatingJavaScriptFromString:. That method will block execution until the JavaScript executes and returns.

For WKWebView, you would need to do something like https://stackoverflow.com/a/30894786/3352624 since its - evaluateJavaScript:completionHandler: doesn't block.

To make old contents "disappear"

If you really just want to make "the old web page to disappear", you could cover the content area of the webview with a blank UIView temporarily. You could hide the contents when you initiate the load and then show the contents using the delegate methods above after the load completes.

Community
  • 1
  • 1
davew
  • 1,216
  • 14
  • 27
1

Swift

webView.load(URLRequest(url: URL(string: "about:blank")!))

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
   //Load request or write code here
})
ZAFAR007
  • 2,873
  • 1
  • 31
  • 42