webview,当前页跳转,右滑返回上一页,而不是直接back

1.给webview添加KVO 检测,因为当前页跳转webview回调方法不能检测到跳转

[self.webView addObserver:self forKeyPath:canGoBackKeyPath options:NSKeyValueObservingOptionNew context:nil];

2.监测方法实现

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:canGoBackKeyPath])
    {
        NSLog(@"URL------%@---%d",_webView.URL.absoluteString,self.webView.canGoBack);
        if (self.webView.canGoBack == TRUE)
        {
            [self leftGesture:NO];
        }
        else
        {
            [self leftGesture:YES];
        }
    }
}

3.添加webview右滑手势禁用可用,falg:NO 不可用 YES 可用

-(void)leftGesture:(BOOL)falg
{
    //禁用屏幕左滑返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
        self.navigationController.interactivePopGestureRecognizer.enabled = falg;
    }
}

4.pragma mark 右滑手势事件

- (void)willMoveToParentViewController:(UIViewController *)parent
{
    [super willMoveToParentViewController:parent];
    if (parent == nil)
    {
        [self da_navigationControllerShouldPopOnBackButton];
    }
}

5.调用webview goBack

- (BOOL)da_navigationControllerShouldPopOnBackButton
{
    if (self.webView.canGoBack) {
        [self.webView goBack];
        return NO;
    } else {
        return YES;
    }
}

你可能感兴趣的:(webview,当前页跳转,右滑返回上一页,而不是直接back)