How to clear back forward list in UIWebview on iPhone?

27,926

Solution 1

Disclaimer
As with anything like this, bear in mind that the results may not make it through app store approval and may not work at-all with future revisions of the SDK.


There is no official method for doing this in the SDK. However if you really want to clear the back/forward history of a UIWebView it can be done with a little delve into the private frameworks.

A quick and dirty way to do it (complete with a bunch of ugly compiler warnings) is as follows:

Given that myUIWebViewInstance is a perfectly normal instance of UIWebView:

id internalWebView=[[myUIWebViewInstance _documentView] webView];       
[internalWebView setMaintainsBackForwardList:NO];
[internalWebView setMaintainsBackForwardList:YES];

There is also the rather tempting _clearBackForwardCache method in the framework, however it didn't seem to do much when tickled. Just flipping the boolian worked a treat for me.

Solution 2

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.

Solution 3

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

Solution 4

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).

Solution 5

I think if you release the uiwebview object and recreate then it would clear the history. I actually have a reverse problem. My uiwebview object automatically gets released during low memory event if it's inside a nonvisible view. I then have to recreate it in viewDidLoad but it does not retain the history from that point onwards.

Share:
27,926
zsniperx
Author by

zsniperx

Updated on July 05, 2022

Comments

  • zsniperx
    zsniperx almost 2 years

    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).

  • zsniperx
    zsniperx over 13 years
    Hi Colin, thank you for your reply. Sadly, I would still need to go back to the first page in the list to do this. Even then, it will only override the pages after the first one. Leaving the fist one there
  • zsniperx
    zsniperx over 13 years
    Very sneaky code! We want the app to get in the app store so this doesn't benefit us. Mark correct because you said there was no official way. Too bad
  • zsniperx
    zsniperx over 13 years
    I see what you mean, but I guess this doesn't exactly fit my situation. It would definitely be the correct answer for if I wan't to disable the ability to go back. My situation is I need to put the UIWebView at its root url so that I can display a toolbar to go with it and have a separate toolbar for any urls on top of it. Thanks for your help though!
  • mharper
    mharper over 12 years
    Great solution in most cases. Seems to break when the URL redirects. Still worth an up vote!
  • Julian D.
    Julian D. over 12 years
    Unfortunately this won't work if programmatic back/forward navigation has been used before: in this case history.length is no longer in sync with the actual history stack and will show the number of user-navigated (by clicking links) pages. history.length will typically be larger than the number of pages you can go back, and history.go(history.length-1) is out of bounds and will quietly fail.
  • bendytree
    bendytree about 12 years
    No doubt about it, I was rejected today for using this exact code.
  • Marsman
    Marsman almost 12 years
    Apple just rejected one of my apps for using this private framework.
  • Anshad Rasheed
    Anshad Rasheed over 8 years
    [internalWebView setMaintainsBackForwardList:NO]; setMaintainsBackForwardList method not found :(
  • Supertecnoboff
    Supertecnoboff almost 7 years
    I tried that, but for whatever reason it lead to slow loading times....
  • Guy Lowe
    Guy Lowe over 6 years
    It also clears the cache which you sometimes don't want to do. For instance, when you want to keep the contents of a shopping cart.
  • Elstine P
    Elstine P almost 6 years
    what do you mean by recreating the webivew ? doing alloc and init again