I am trying to load data directly into UIWebView:
[webView loadData:data MIMEType:@"text/html" textEncodingName:@"utf-8"
baseURL:nil];
The data is some html string contains some external links. I am trying to enable the back and forward buttons base on the UIWebView properties:
self.forwardBtn.enabled = self.webView.canGoForward;
self.backBtn.enabled = self.webView.canGoBack;
However, even after I clicked on different links in the web view after the initial load, the webView.canGoForward and webView.canGoBack are always returning NO.
Any idea about this?
Ok, I think the problem is if I use loadData or loadHTMLString, and then I check the url of the request in the delegate:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"url=%@", [request URL]);
return YES;
}
It seems like that url is empty. I think this is why there is no history for goBack when using loadData.
Easy quick solution, drag and drop bar button items to the xib, set image and untick the Enabled box from the Attribute Inspector.
// WebViewController.h
@interface WebViewController : UIViewController <UIWebViewDelegate, UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *goBack;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *goForward;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
// WebViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
if ([ReachabilityTest networkConnected] == YES)
{
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlString]]];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection!" message:@"Please, make sure you have internet connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
- (void)webViewDidStartLoad:(UIWebView *)_webView {
[self.activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)_webView {
[self.activityIndicator stopAnimating];
self.goBack.enabled = [self.webView canGoBack];
self.goForward.enabled = [self.webView canGoForward];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With