问题
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?
回答1:
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.
回答2:
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];
}
回答3:
The reason is loadHTMLString doesn't add this html file to the hierarchy of transitions, even when you add some baseURL. So you need to use loadRequest for normal navigation on the page hierarchy.
let htmlFile = "file"
guard let localfilePath = Bundle.main.path(forResource: htmlFile, ofType: "html"),
let url = URL(string: localfilePath) else { return }
view.loadRequest(URLRequest(url: url))
回答4:
Use my code, i use this code for my custom webview. This is not the perfect answer to your question but it will help. Sorry for formatting :)
- (IBAction)backwordButton:(id)sender {
[webViewOutlet goBack];}
- (IBAction)forwordButton:(id)sender {
[webViewOutlet goForward];
}
- (IBAction)stopButton:(id)sender {
[webViewOutlet stopLoading];
}
- (IBAction)refreshButton:(id)sender {
[webViewOutlet reload];
}
- (IBAction)backButton:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated {
UIImage *goBackImageChange = [UIImage imageNamed:@"browserBackInactive@2x.png"];
[goback setBackgroundImage:goBackImageChange forState:UIControlStateNormal];
UIImage *goForwordImageChange = [UIImage imageNamed:@"browserForwardInactive@2x.png"];
[goforowrd setBackgroundImage:goForwordImageChange forState:UIControlStateNormal];
NSURL *urlToLoad = [[NSUserDefaults standardUserDefaults] URLForKey:@"URLToLoad"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlToLoad];
[webViewOutlet loadRequest:requestObj];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
activityIndicator.hidden = NO;
[activityIndicator startAnimating];
}
- (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;
}
来源:https://stackoverflow.com/questions/8810412/uiwebview-cangoback-and-cangoforward-always-return-no