How to clear back forward list in UIWebview on iPhone?

前端 未结 8 463
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-10 13:09

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:

         


        
相关标签:
8条回答
  • 2020-12-10 14:02

    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.

    0 讨论(0)
  • 2020-12-10 14:08

    I've tried almost every solution posted without much success. My case is for Childbrowser phonegap plugin, but is based in the same problem, a single webview instance used to show external pages in my application. When I show an external page, the webview maintains the history of previous pages shown. I wanted to remove the history when I show a new external webpage.

    After some research, I've found a solution in this SO post that worked for me.

    [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML = \"\";"];
    

    In the case of childbrowser, I placed it in the method -(IBAction) onDoneButtonPress:(id)sender, which previously inserted a blank page to start blank (but with back button enabled).

    HTH! Milton.

    0 讨论(0)
提交回复
热议问题