iOS开发笔记-56:WKWebView的进度条和回退上级网页以及网页标题

WKWebView的进度条
WKWebView的回退上级网页
WKWebView的获取网页标题

[获取系统返回按钮的点击事件的问题我不赘述了,点击跳转]


//获取系统返回按钮的点击事件
-(BOOL)navigationShouldPopOnBackButton {
//    [JJTools lightNavigation:self];
    if (self.webView.canGoBack==YES) {
        [self.webView goBack];
        return NO;
    }else{
        return YES;
    }
}

 //进度条
    _progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame),2)];
    _progressView.tintColor = _kMainColor;
    _progressView.trackTintColor= _kmainBackGroundColor;
    
    [self.view addSubview:_progressView];
    [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew| NSKeyValueObservingOptionOld context:nil];
   
   //监听标题网页标题
    if (_shouldUpdataTitle) {
        [_webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
    }

//添加网页加载进度条获取标题
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    NSLog(@" %s,change = %@",__FUNCTION__,change);
    
    if ([keyPath isEqual: @"estimatedProgress"] && object == _webView) {
        [self.progressView setAlpha:1.0f];
        [self.progressView setProgress:_webView.estimatedProgress animated:YES];
        if(_webView.estimatedProgress >= 1.0f)
        {
            [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
                [self.progressView setAlpha:0.0f];
            } completion:^(BOOL finished) {
                [self.progressView setProgress:0.0f animated:NO];
            }];
        }
    } else if ([keyPath isEqualToString:@"title"])
    {
        if (object == self.webView) {
            self.title = self.webView.title;

        }
        else
        {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

        }
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

- (void)dealloc {
    [_webView removeObserver:self forKeyPath:@"estimatedProgress"];
    //监听标题网页标题
    if (_shouldUpdataTitle) {
        [_webView removeObserver:self forKeyPath:@"title"];
    }

    // if you have set either WKWebView delegate also set these to nil here
    [_webView setNavigationDelegate:nil];
    [_webView setUIDelegate:nil];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容