43

I am very new to the whole programming business, and was wondering if there is any way to clear the contents of a UIWebView in iphone programming, so that the loading symbol for the next view is not showing up in front of the last view. Many Thanks, Thomas

6 Answers 6

149

Try setting the URL to about:blank and reload the page.

3
  • 2
    Thanks Mate, I Would vote u up but I dont have enough rank myself
    – Tom Cuzz
    Commented Jul 4, 2011 at 21:56
  • 3
    Know of an alternative that doesn't result in the webViewDidFinishLoad delegate method firing? (nil'ing delegate isn't workable: clear is followed immediately by another load of real content, so have have delegate ready in case that responds quickly... so would need to turn off delegate until after first load, then turn on before second load, but it's all async so I can't block until first load is finished). Commented Mar 11, 2013 at 23:34
  • @BillPatterson Just check the location/url.
    – mattsven
    Commented Nov 12, 2013 at 16:58
27

Just load an empty html string into it

[self.webView loadHTMLString:@"" baseURL:nil];

4
  • no need to set a 'baseURL' ... [self.webView loadHTMLString:@"" baseURL:nil]; Commented Jul 16, 2013 at 21:34
  • @ChrisAllinson sure I simplified it according to your suggestion Commented Nov 1, 2013 at 2:29
  • I've found that it doesn't work unless I do specifically pass a baseURL Commented Jun 6, 2014 at 10:19
  • For me this passed A JavaScript exception occurred about a million times so I changed it lol it worked tho!
    – Michael
    Commented Feb 2, 2021 at 18:17
10

Answer extension for documentation purposes to maybe help someone else:

I had the same desire (clear content before loading next url) but had a UIWebView delegate set to receive webviewDidFinishLoad:(UIWebView)webview message and update another part of UI in response.

Problem: the call to clear the content to also called delegate method, so getting false-hits (that is, getting call when clear is done, too, but delegate is coded to expect call only when real content is loaded).

Solution: use a known URL for clear, and have webviewDidFinishLoad: ignore calls made when that URL is finished:

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

    // Load real next URL
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"WebView finished loading: %@", webView);
    if ([self.webView.request.URL.absoluteString isEqualToString:@"about:blank"]) {
        NSLog(@"  This is Blank. Ignoring as false event.");
    }
    else {
        NSLog(@"  This is a real url");
        [self updateUIInSomeWay];
    }
}

Note: using this:

    [self.webView loadHTMLString:@"about:blank" baseURL:nil];

actually causes the words "about:blank" to appear as text in the webview's content pane!

Final complication: In practice, with my two [webview load...] calls so close together, I was finding that instead of a "loaded" event for the clear, the webview was actually canceling it in favor of the second request and calling webView: didFailLoadWithError: for the first load request. Thus, I had to put similar code in that event:

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"WebView error on: %@", webView);
    NSLog(@"Error is: %@", error);

    NSURL* failingURL = [error.userInfo objectForKey:@"NSErrorFailingURLKey"];
    if ([failingURL.absoluteString isEqualToString:@"about:blank"]) {
        NSLog(@"  This is Blank. Ignoring.");
    }
    else {
        NSLog(@"  This is a real URL.");
        [self doSomethingAboutError];
    }
}
5

Swift, Xcode 7 beta 5

webView.loadRequest(NSURLRequest(URL: NSURL(string: "about:blank")!))
0
3

Swift 4.0 , XCODE 9

webView.loadRequest(URLRequest.init(url: URL.init(string: "about:blank")!))
2

Same answer in Swift 4.2, xCode 10

if let clearURL = URL(string: "about:blank") {
   myWebView.loadRequest(URLRequest(url: clearURL))
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.