4

I am showing a some text and images in UIwebview in navigation based application. It also contains some link to the source of the data i.e the websites. It is showing it in perfect manner. But when user click on the back button it pop the previous viewcontroller and goes to it. But, What I want when user click on the link the back button must be converted into browser's back button and act like it.

Any suggestions/sample code or tutorial for it?

Sanchit Paurush
  • 6,114
  • 17
  • 68
  • 107

5 Answers5

17

You can replace the navigation bar's back button with a browser back button whenever the UIWebView has the option to go back by doing something like this:

- (void)updateBackButton {
    if ([self.webView canGoBack]) {
        if (!self.navigationItem.leftBarButtonItem) {
            [self.navigationItem setHidesBackButton:YES animated:YES];
            UIBarButtonItem *backItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backWasClicked:)] autorelease];
            [self.navigationItem setLeftBarButtonItem:backItem animated:YES];
        }
    }
    else {
        [self.navigationItem setLeftBarButtonItem:nil animated:YES];
        [self.navigationItem setHidesBackButton:NO animated:YES];
    }
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [self updateBackButton];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self updateBackButton];
}

- (void)backWasClicked:(id)sender {
    if ([self.webView canGoBack]) {
        [self.webView goBack];
    }
}
cduhn
  • 17,818
  • 4
  • 49
  • 65
2

See First thing that you have to do is, that you have to hide the default back button, as follows below

self.navigationItem.hidesBackButton = YES;

After this you have to add a custom back button on the navigationbar as below:

UIButton *m_BackBtn = [UIButton buttonWithType:UIButtonTypeCustom];

m_BackBtn.frame = CGRectMake(4.0, 5.0+0.0, 100, 30);

[m_BackBtn setTitle:@"Back" forState:UIControlStateNormal];

[m_BackBtn addTarget:selfaction:@selector(BackButtonAction)forControlEvents:UIControlEventTouchUpInside];

[m_BackBtn retain];

[self.navigationController.navigationBar addSubview:m_BackBtn];

*And in the BackButtonAction Function *

-(void)BackButtonAction
{

   [yourwebView goBack];

// Do your stuff as you required

}

sumanthkodi
  • 196
  • 2
  • 3
  • 7
  • I hope this will help you, if this above code helped you means please vote me up – sumanthkodi Aug 09 '11 at 05:52
  • But in my designs I need to put the default back button of navigation controller Coz I need to show the previous viewconrtoller name in the back when it is inside a view controller – Sanchit Paurush Aug 09 '11 at 06:06
0

I like @cduhn's approach above for it's simplicity, but it has the downside of losing the "<" on the Back button. See this answer if you want to keep the "real" back button (with it's "<" icon), but change the button's behavior:

https://stackoverflow.com/a/19132881/132542

For the web view history case here, this is the code I used in my delegate method:

-(BOOL) navigationShouldPopOnBackButton {
    if (webView.canGoBack) {
        // Then just go back in the web view
        [webView goBack];
        // return NO to keep the current view controller loaded
        return NO;
    }
    else {
        // no web history, so pop the view
        return YES;
    }
}
Community
  • 1
  • 1
Ryan
  • 606
  • 5
  • 12
0

This is a bit tricky solution, may not be suggested. But this way you can maintain the back navigation animation on navigation bar.

var urlRequest: NSURLRequest?
////////
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    if navigationType == .LinkClicked {
        //AViewController creates and pushes itself to the navigationController
        let pushViewController = self.storyboard?.instantiateViewControllerWithIdentifier("AViewController") as! AViewController
        pushViewController.urlRequest = request
        self.navigationController?.pushViewController(pushViewController, animated: true)
        return false
    } else { //is initial request from push
        return true
    }
} 
osrl
  • 8,168
  • 8
  • 36
  • 57
0

Take a UIBarbuttonItem from XIB at the place of back button and you can write following in its IBAction method Suppose your UIWebview object is

UIWebView *web;

-(IBAction)goBack
{
       [web goBack];
}

goBack is inbuilt method of UIWebView

Dimple
  • 406
  • 3
  • 11