Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I clear the contents of a UIWebView/WKWebView?

Tags:

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]; } 
like image 484
Halpo Avatar asked Oct 20 '15 10:10

Halpo


People also ask

How do I reset WKWebView?

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.

What is the difference between UIWebView and WKWebView?

Difference Between UIWebview and WKWebViewUIWebview is a part of UIKit, so it is available to your apps as standard. You don't need to import anything, it will we there by default. But WKWebView is run in a separate process to your app,. You need to import Webkit to use WKWebView in your app.

Is WKWebView the same as Safari?

WKWebView - This view allows developers to embed web content in your app. You can think of WKWebView as a stripped-down version of Safari. It is responsible to load a URL request and display the web content. WKWebView has the benefit of the Nitro JavaScript engine and offers more features.

What is WKWebView in Swift?

A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.


2 Answers

You can write below code while your controller dismiss.

webView.load(URLRequest(url: URL(string:"about:blank")!)) 
like image 119
Donal Avatar answered Oct 25 '22 01:10

Donal


You can make it load a blank page

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]]; 
like image 42
ogres Avatar answered Oct 25 '22 02:10

ogres