UIWebView canGoBack和canGoForward总是返回NO

时间:2012-01-10 20:57:37

标签: objective-c ios cocoa-touch uiwebview

我正在尝试将数据直接加载到UIWebView

[webView loadData:data MIMEType:@"text/html" textEncodingName:@"utf-8" 
          baseURL:nil];

数据是一些html字符串包含一些外部链接。我正在尝试根据UIWebView属性启用后退和前进按钮:

self.forwardBtn.enabled = self.webView.canGoForward;
self.backBtn.enabled = self.webView.canGoBack;

但是,即使我在初次加载后点击了网页视图中的不同链接,webView.canGoForwardwebView.canGoBack也始终会返回NO

对此有何想法?

4 个答案:

答案 0 :(得分:4)

好的,我认为问题是如果我使用loadData或loadHTMLString,然后我在委托中检查请求的url:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"url=%@", [request URL]);
    return YES;
}

好像该网址是空的。 我认为这就是使用loadData时没有goBack历史的原因。

答案 1 :(得分:1)

轻松快速解决方案,将条形按钮项拖放到xib,设置图像并从“属性”检查器中取消选中“已启用”框。

// 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];
}

答案 2 :(得分:1)

原因是 loadHTMLString 无法将此html文件添加到转换层次结构中,即使您添加了一些baseURL也是如此。因此,您需要在页面层次结构上使用 loadRequest 进行常规导航。

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))

答案 3 :(得分:0)

使用我的代码,我将此代码用于我的自定义webview。这不是你问题的完美答案,但它会有所帮助。抱歉格式化:)

- (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;
}