i am new to iphone. i want to disable Back(left arrow button) and Forward(right arrow button) button on UIToolBar in UIWebView. After moved web page want enable back button and then Forward button want to enable when its need. in my app, i am using toolbar at bottom side via interface builder. please any one help me!
5 Answers
Send the following message to the button object to enable/disable:
// Enable
[myButton setEnabled:YES];
// Disable
[myButton setEnabled:NO];
To determine whether you should show these buttons you should check the following properties on the webview:
[myWebView canGoBack];
[myWebView canGoForward];
You'll want to check these whenever the user loads a page. To do so implement the UIWebViewDelegate method webViewDidFinishLoad:
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
// Enable or disable back
[myBackButton setEnabled:[myWebView canGoBack]];
// Enable or disable forward
[myForwardButton setEnabled:[myWebView canGoForward]];
}

- 34,311
- 8
- 67
- 88

- 7,461
- 2
- 45
- 50
Just implement in One of the delegate method of webView.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if ([webView canGoBack])
[backbutton setEnabled:YES];
else
[backbutton setEnabled:NO];
if ([webView canGoForward])
[fwdbutton setEnabled:YES];
else
[fwdbutton setEnabled:NO];
}

- 3,895
- 4
- 45
- 61

- 553
- 5
- 9
Other than pheelicks answer, I had to make the following change to disable back button when you comes back to first page and forward button when you go last page.
Added an observer on WebHistoryItemChangedNotification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(webViewHistoryDidChange:)
name:@"WebHistoryItemChangedNotification"
object:nil];
updating button states in webViewHistoryDidChange.
- (void)webViewHistoryDidChange
{
if ([theWebView canGoBack])
{
self.backButton.enabled = YES;
}
else
{
self.backButton.enabled = NO;
}
if ([theWebView canGoForward])
{
self.forwardButton.enabled = YES;
}
else
{
self.forwardButton.enabled = NO;
}
}
Refer : Same is answered here
-
Thank you so much. Not the cleanest solution that exists. but definitely working well !!! – touti Jun 26 '19 at 14:16
For the developers who are struggling to address this in WKWebView
, you need to implement KVO to observe canGoBack
and canGoForward
properties of WKWebView
in the viewDidLoad
method.
webView.addObserver(self, forKeyPath: #keyPath(WKWebView.canGoBack), options: .new, context: nil)
webView.addObserver(self, forKeyPath: #keyPath(WKWebView.canGoForward), options: .new, context: nil)
And in the observer method
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == #keyPath(WKWebView.canGoBack) || keyPath == #keyPath(WKWebView.canGoForward) {
backButton.isEnabled = webView.canGoBack
forwardButton.isEnabled = webView.canGoForward
}
}
Also, don't forget to remove the observer when the ViewController is getting deallocated.

- 1,720
- 1
- 16
- 23
A two liner solution for Swift:
func webViewDidFinishLoad(_ webView: UIWebView) {
btnBack.isEnabled = webView.canGoBack
btnForward.isEnabled = webView.canGoForward
}

- 3,545
- 2
- 29
- 33
-
-
It should return false when going back one level on your navigation, unless maybe the page you are navigating has hash style navigation or something. Try with another site just to be sure. – JoeGalind Aug 24 '21 at 22:33