Monday, June 3, 2013

UIWebView canGoBack example in Objective C (iOS).


UIWebView canGoBack

A Boolean value indicating whether the receiver can move backward. (read-only)

@property(nonatomic, readonly, getter=canGoBack) BOOL canGoBack

Discussion of [UIWebView canGoBack]
If YES, able to move backward; otherwise, NO.

UIWebView canGoBack example.
- (void) webViewDidFinishLoad:(UIWebView *)webView {
    if ([resultsWebView canGoBack]) {
        [goBackButton setEnabled:YES];
    }
    else {
        [goBackButton setEnabled:NO];
    }
    if ([resultsWebView canGoForward]) {
        [goForwardButton setEnabled:YES];
    }
    else {
        [goForwardButton setEnabled:NO];
    }
}

Example of [UIWebView canGoBack].
- (void)webViewDidFinishLoad:(UIWebView *)webView {

    BOOL ableToGoBack = [webViewOutlet canGoBack];
    BOOL ableToGoForword = [webViewOutlet canGoForward];

    if (ableToGoBack == YES) {
        UIImage *goBackImageChange = [UIImage imageNamed:@"browserBack@2x.png"];
        [goback setBackgroundImage:goBackImageChange forState:UIControlStateNormal];
    } else {
        UIImage *goBackImageChange = [UIImage imageNamed:@"browserBackInactive@2x.png"];
        [goback setBackgroundImage:goBackImageChange forState:UIControlStateNormal];
    }
    if (ableToGoForword == YES) {
        UIImage *goForwordImageChange = [UIImage imageNamed:@"browserForward@2x.png"];
        [goforowrd setBackgroundImage:goForwordImageChange forState:UIControlStateNormal];
    }  else {
        UIImage *goForwordImageChange = [UIImage imageNamed:@"browserForwardInactive@2x.png"];
        [goforowrd setBackgroundImage:goForwordImageChange forState:UIControlStateNormal];
    }

    [activityIndicator stopAnimating];
    activityIndicator.hidden = YES;
}

End of UIWebView canGoBack example article.