Search code examples
objective-cioscocoa-touchuiwebview

UIWebView canGoBack and canGoForward always return NO


I am trying to load data directly into UIWebView:

[webView loadData:data MIMEType:@"text/html" textEncodingName:@"utf-8" 
          baseURL:nil];

The data is some html string contains some external links. I am trying to enable the back and forward buttons base on the UIWebView properties:

self.forwardBtn.enabled = self.webView.canGoForward;
self.backBtn.enabled = self.webView.canGoBack;

However, even after I clicked on different links in the web view after the initial load, the webView.canGoForward and webView.canGoBack are always returning NO.

Any idea about this?


Solution

  • Ok, I think the problem is if I use loadData or loadHTMLString, and then I check the url of the request in the delegate:

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
        NSLog(@"url=%@", [request URL]);
        return YES;
    }
    

    It seems like that url is empty. I think this is why there is no history for goBack when using loadData.