1

I have a UIWebView that I use to load a webpage. I also have navigation buttons so you can go back and forward between previous pages loaded. Is there a way to hide the navigation buttons when there is no previous webpage?

user3180303
  • 15
  • 1
  • 4

3 Answers3

1

Check here: Why is UIWebView canGoBack=NO in iOS7?

You can enable/disable your navigation buttons in the shouldStartLoadWithRequest method with canGoBack and canGoForward methods on UIWebView:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType
 {
    if ([webView canGoBack])
    {
        [_browserBackItem setEnabled:YES];
    }
    else
    {
        [_browserBackItem setEnabled:NO];
    }
    if ([webView canGoForward])
    {
        [_browserForwardItem setEnabled:YES];
    }
    else
    {
        [_browserForwardItem setEnabled:NO];
    }
    return YES;
}
Community
  • 1
  • 1
Damien
  • 660
  • 6
  • 15
  • This will actually work, only thing is how i have my forward and back buttons to control the UIWebView is not through my implementation files i do it by right clicking and dragging from the webview to the button and then select the action. What should i use to implement them into my .m file? – user3180303 Jan 12 '14 at 00:30
  • I'm not sure I understand what you mean. Could you post a screen capture? You need to link your buttons from your .xib file to your class and add an IBOutlet in your .h file so you can access them in your .m file. – Damien Jan 12 '14 at 00:41
  • Ok on the left is my .h and the right is .m, http://i.imgur.com/gR3oqwy.png and i have connected the go back and gofoward to the webview – user3180303 Jan 12 '14 at 00:58
  • Replace goBack and go forward by self.goback and self.goforward and it should be fine. – Damien Jan 12 '14 at 01:03
  • Replace them in the .m file correct? under the - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType I did this but it still gets an error – user3180303 Jan 12 '14 at 01:06
  • Yes, replace them where you had errors in your screen cap. I don't get it, you shouldn't get any error. What does the error says? – Damien Jan 12 '14 at 01:10
  • Nope, do you have a source code? If not, I think it might be because i dont have the back and forward buttons in code in the .m. what would i use to enable the back and forward in code? – user3180303 Jan 12 '14 at 01:21
  • Actually, you can't hide a UIBarButtonItem and have to enable/disable or add/remove it (I edited my answer). That's why you still get an error? Here's a demo project: https://www.dropbox.com/s/9n351f5lpe0t4zo/WebViewTest.zip – Damien Jan 12 '14 at 01:45
  • Thank you! Finally got it to work. Ran into one problem though, its not a huge one but the first time you go from the main page it doesn't allow the back button but the second time it does. – user3180303 Jan 12 '14 at 02:16
0

I'm not aware of anything built in to UIWebView that allows this but maybe you could try keeping track of the pages with a variable.

Every time a url request is made the UIWebView delegate method gets called and you could increment the variable there (see here). Then decrement the variable once the user has selected the back button.

Hope that helps.

nalyd88
  • 4,940
  • 8
  • 35
  • 51
0

you can save the loaded pages in an NSArray for example and test if that array is empty you hide the button.

to know the opened pages, you implement the UIWebViewDelegate in your class, and in the – webView:shouldStartLoadWithRequest:navigationType: callback you save the url.

Red Mak
  • 1,176
  • 2
  • 25
  • 56