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

Tom Cuzz
  • 675
  • 1
  • 5
  • 10

6 Answers6

149

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

Constantino Tsarouhas
  • 6,846
  • 6
  • 43
  • 54
  • 2
    Thanks Mate, I Would vote u up but I dont have enough rank myself – Tom Cuzz Jul 04 '11 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). – Bill Patterson Mar 11 '13 at 23:34
  • @BillPatterson Just check the location/url. – mattsven Nov 12 '13 at 16:58
27

Just load an empty html string into it

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

Minebomber
  • 1,209
  • 2
  • 12
  • 35
2cupsOfTech
  • 5,953
  • 4
  • 34
  • 57
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];
    }
}
Bill Patterson
  • 2,495
  • 1
  • 19
  • 20
5

Swift, Xcode 7 beta 5

webView.loadRequest(NSURLRequest(URL: NSURL(string: "about:blank")!))
neoneye
  • 50,398
  • 25
  • 166
  • 151
3

Swift 4.0 , XCODE 9

webView.loadRequest(URLRequest.init(url: URL.init(string: "about:blank")!))
ViJay Avhad
  • 2,684
  • 22
  • 26
2

Same answer in Swift 4.2, xCode 10

if let clearURL = URL(string: "about:blank") {
   myWebView.loadRequest(URLRequest(url: clearURL))
}
Abdul Rehman
  • 2,386
  • 1
  • 20
  • 34