iOS uiwebview goBack history control

21,699

UIWebviews have instance methods that allow you to go backwards and forwards with the history, without you having to track the last request. Is there a particular reason you are saving the last request specifically?

Sample code:

if ([webView canGoBack]) {
    [webView goBack];
}

Here is a project that is a simple uiwebview browser if you want to play with it: https://github.com/msencenb/UIWebView-Example

Share:
21,699
Jaume
Author by

Jaume

Updated on January 26, 2020

Comments

  • Jaume
    Jaume over 4 years

    I am loading a "webpage1" on an uiwebview and saving its first request on an instance variable "webviewHistory". Then, I must reuse that webview to load another page, "webpage2" (saved its first request to "webviewHistory" too), and history should start now from this request. Problem is if I execute goBack (button IBAction) from "webpage2", I can keep going back to "webpage1" history and following. If I check request and compare with initial saved, works! but not with redirections, for example, if I trigger request to www.youtube.com, request is redirectioned to m.youtube.com and first one is not saved as navigation history! how to solve it?

    if (![webViewSocial.request.URL.absoluteString isEqualToString:self.webviewHistory]){
        [webViewSocial goBack];
        }
    
  • Jaume
    Jaume almost 12 years
    I am already using this methods but I need to enable or disable goBack action depending if request is older that first loaded page!
  • Msencenb
    Msencenb almost 12 years
    Ok. I think you should wait for redirection to happen on first page (so you get the correct first page loaded) using uiwebview delegate method: webViewDidFinishLoad: then save to your instance variable.