Search code examples
iphoneipaduiwebview

How to clear back forward list in UIWebview on iPhone?


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


Solution

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