Simple concept, however it's not working. I have two buttons in my UIWebView
, a back button, and a forward button. Back buttons calls [self.webView goBack] and forward [self.webView goForward].
Now I want to know basically if the UIWebView
can go back. If so, then enable the back button, and let it go back, and the same for the forward button.
-(void)webViewDidStartLoad:(UIWebView *)webView
{
if([self.webView canGoForward]) {
[forwardArrowBtn setEnabled:YES];
} else {
[forwardArrowBtn setEnabled:NO];
}
if([self.webView canGoBack]) {
[backArrowBtn setEnabled:YES];
} else {
[backArrowBtn setEnabled:NO];
}
}
Problem is this doesn't work on the first load. For example: I click on a link within the UIWebView
, it goes to the page, but the back button is still disabled and not clickable. Not until I click on another link will the back button show enabled and start working.
How come this doesn't work the first time you go to a different page, and only works the second time?
I solved this issue in putting the button setting code not only in webViewDidStartLoad. Write a method and call it from webViewDidStartLoad, webViewDidFinishLoad and shouldStartLoadWithRequest. Works flawlessly with my app.
Is your first page an actual web location or file on your device? I have this issue when I'm starting with loadHTMLString, presumably because there really isn't anything to go back to (I'm assuming the UI doesn't cache the page content, just the reference to the page). When I'm working with a loadRequest, it works fine. That said, you do need to do as Aemsn stated. A good tutorial can be found at this link.
Having come here while doing a random web search, to answer the 'base' question of why it only works the second time:
webViewDidStartLoad gets called before you actually load the next page. For what you're doing, webViewDidFinishLoad makes more sense. So when you do your first load, you can't go back (nothing loaded for forward or back). When you change pages the first time, you still can't -- only one page (current) is loaded. Thus, the second time onward is when it appears to work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With