Wkwebview cangoback 给出了错误的结果

Wkwebview cangoback is giving wrong results

如果 canGoBack 为真,我将在网络视图中显示后退按钮。

它工作正常,但对于一个特定的网页,即使没有后页可返回,webview 说 cangoback 为 true 而 cangoback 应该是 false。


更新

这是初始化我的网络视图的代码片段

-(void)initWebView{    
    WKUserContentController* userContentController = WKUserContentController.new;
    NSString* documentCookie = [self documentCookie];
    WKUserScript * cookieScript = [[WKUserScript alloc]
                                   initWithSource: documentCookie
                                   injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
    // again, use stringWithFormat: in the above line to inject your values programmatically
    [userContentController addUserScript:cookieScript];
    WKWebViewConfiguration* webViewConfig = WKWebViewConfiguration.new;
    webViewConfig.userContentController = userContentController;
    self.webviewObj = [[WKWebView alloc] initWithFrame:CGRectMake(self.vuParent.frame.origin.x, self.vuParent.frame.origin.y, self.vuParent.frame.size.width, self.vuParent.frame.size.height) configuration:webViewConfig];
    self.webviewObj.UIDelegate = self;
    [self.webviewObj setBackgroundColor:[UIColor whiteColor]];

    self.webviewObj.translatesAutoresizingMaskIntoConstraints=false;

    [self.vuParent addSubview:self.webviewObj];

}

这是我在我的网络视图中加载请求的方式

-(void)completeWebRequest
{
    NSString* urlToRequestStr = @"";


    if ([[self targetUrl] length]) {
        urlToRequestStr = [self targetUrl];
    }


    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: urlToRequestStr]];



    if (self.isPOST) {

        NSMutableData *body = [[NSMutableData alloc] initWithData:[self.postParams dataUsingEncoding:NSUTF8StringEncoding]];
        NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[body length]];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setHTTPBody:body];
    }

    [self initWebView];
    self.webviewObj.scrollView.scrollEnabled=false;
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.webviewObj setOpaque:false];


    self.webviewObj.navigationDelegate = self;



    [self setOriginalUrlToRequest:tempRequest];
    [[self webviewObj] loadRequest:tempRequest];
}

这是我检查是否需要显示后退按钮的方法。

  [self setBackButtonCheckTimer:[NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(chkBackBtn) userInfo:nil repeats:true]];

- (void) chkBackBtn {

    if ([[self webviewObj] canGoBack]) {
        [[self navigationItem] setLeftBarButtonItem:[self bkButton]];
    }
    else{
        [[self navigationItem] setLeftBarButtonItem:nil];
    }
}

有一种方法可以用来调试此类问题。 canGoBack 方法基于 backForwardList.

工作

您可以验证列表中的内容并进行更改。