Alex Rivera | Logout

How to clear back forward list in UIWebview on iPhone?

Asked 2010-09-26T21:35:37.480
12

I want to access/clear the back forward list as if the UIWebView is new again. Is there any public API or workaround to do this?

I've tried:

while ([webview canGoback]) {
    [webview goBack];
}

but that will freeze the device (simulator too).

Edit
Report

3 Answers

13

If you're trying to "reuse" an existing UIWebView, and disable the ability to go back to whatever previous page it was in before, you can:

  1. When loading the new request (the one that user shouldn't go back from), save the URL of the new request, say like:

    self.curURL = [NSURL urlWithString:@"http://www.bla.com"];
    [webview loadRequest:[NSURLRequest requestWithURL:curURL]];

  2. I'm guessing you have your own back button, so on your webViewDidFinishLoad delegate method, add:

    backbttn.enabled = webView.canGoBack && ![[webView.request URL] isEqual: curURL];

This way, the user won't even know that the UIWebView can go back to the page before.

answered 2011-01-13T16:18:13.840
5

I was successful (as far as you can call such a workaround 'successful') using the following line of code:

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"if( window.history.length > 1 ) { window.history.go( -( window.history.length - 1 ) ) }; window.setTimeout( \"window.location.replace( '%@' )\", 300 );", targetLocation]];

where targetLocation is the desired URL as a NSString.

What it does is to tell the browser to go as far backwards as the history goes and then, after a short timeout, load the desired URL.

This of course does not solve the problem of clearing the forward going history (which I didn't need in my case).

answered 2011-11-04T19:21:00.747
5

Just recreate the webview. it would reset everything including history.

answered 2013-03-15T15:58:51.067

Your Answer