如果canGoBack为true,则在Web视图中显示后退按钮。

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

更新资料

这是初始化我的webview的代码片段

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

}

这是我如何在Webview中加载请求
-(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起作用。

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

关于ios - WkWebView Cangoback给出错误的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61139755/

10-11 06:05