I'm showing back button in webview if canGoBack is true.
It works fine but for one particular webpage even though there's no back page to go back, the webview says cangoback as true while cangoback should have been false.
Update
Here is the code snippet to init my 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];
}
Here is how I load a request in my 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];
}
Here is how I check if back button needs to be shown or not.
[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];
}
}