9

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!

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
Sivanathan
  • 1,213
  • 3
  • 13
  • 25

5 Answers5

28

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]];

 }
Stefan Arentz
  • 34,311
  • 8
  • 67
  • 88
pheelicks
  • 7,461
  • 2
  • 45
  • 50
4

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];
}
James Zaghini
  • 3,895
  • 4
  • 45
  • 61
Gaurav
  • 553
  • 5
  • 9
2

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

Community
  • 1
  • 1
Kvk
  • 522
  • 5
  • 15
  • Thank you so much. Not the cleanest solution that exists. but definitely working well !!! – touti Jun 26 '19 at 14:16
2

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.

Vineeth
  • 1,720
  • 1
  • 16
  • 23
0

A two liner solution for Swift:

func webViewDidFinishLoad(_ webView: UIWebView) {
    btnBack.isEnabled = webView.canGoBack
    btnForward.isEnabled = webView.canGoForward
}
JoeGalind
  • 3,545
  • 2
  • 29
  • 33
  • for me canGoForward always return false can you help me? – Yogesh Patel Aug 24 '21 at 06:47
  • 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