Я показываю кнопку «Назад» в веб-просмотре, если canGoBack
имеет значение «истина».
Она отлично работает, но для одной конкретной веб-страницы, даже если нет обратной страницы для go назад, в веб-представлении говорится, что cangoback имеет значение true в то время как cangoback должен был быть ложным.
Обновление
Вот фрагмент кода для запуска моего веб-просмотра
-(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];
}
}