How to clear back forward list in UIWebview on iPhone?

前端 未结 8 473
爱一瞬间的悲伤
爱一瞬间的悲伤 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 13:48

    Picking up Gilbert's clever approach: If modified, it does work with redirects (something it isn't capable of so far, as mharper pointed out).

    Before loading the request, save the desired URL and set a boolean member variable called _saveURL to indicate that the redirect URL shall be saved (you will see the exact use of these two variables later):

    - (void)my_loadURL:(NSURL *)url
    {
        NSURLRequest *request = [NSURLRequest requestWithURL:url]; // create the request
        [_desiredURL release]; // clear up the previous value (assuming -my_loadURL: may be called multiple times)
        _desiredURL = [url retain]; // store the desired URL (will be used later)
        _saveURL = YES; // will also be used later
        [_webView loadRequest:request]; // start loading the request
    }
    

    (The retain and release calls of course won't be necessary if compiling in an Automatic Reference Counting (ARC) environment.)

    Now in the -webViewDidFinishLoad: delegate callback, check to see whether the redirect has already occured by testing whether the URL of the web view's current request differs from the desired URL. If so, save the redirect URL in a member variable _firstURL. This is also where _saveURL gets into place. It is to avoid overwriting _firstURL each time this delegate method is called. Also, enable or disable the back and forward buttons just the way we did before.

    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
        // grab the URL currently being loaded
        NSURL *currentURL = [webview.request URL];
    
        // check whether we are supposed to save the redirect URL and
        // whether redirection has taken place yet
        if (_saveURL && ![currentURL isEqual:_desiredURL]) {
            [_firstURL release];
            _firstURL = [currentURL retain];
        }
    
        // adjust the enabled-state of the back and forward buttons just like before
        _backButton.enabled = _webView.canGoBack && ![currentURL isEqual:_firstURL];
        _forwardButton.enabled = _webView.canGoForward;
    }
    

    (Again, retain and release are not needed with ARC enabled.)

    However, there's one key disadvantage of this method. It only works if you know for sure that the URL passed into my_loadURL: will redirect. Otherwise, the _firstURL variable will be set somewhen else. So if you can't tell whether the according URL will redirect, then this approach does not fit your needs. Anyway, it did fit my needs, and I hope I could help out someone else as well.

    Update: You can improve this method by dropping all of that dealing with _desiredURL, meaning don't save the desired URL in -my_loadURL: and in -webViewDidFinishLoad: just say if(_saveURL). This way, it'll work for websites that either don't redirect at all or that redirect instantly.

提交回复
热议问题