美文网首页iOS开发webView
iOS WKWebView 获得title和进度

iOS WKWebView 获得title和进度

作者: sttech | 来源:发表于2017-06-15 14:43 被阅读0次

iOS 获取webView的title

    [self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        if (object == webView) {
            [self.progressView setAlpha:1.0f];
            [self.progressView setProgress:self.currentSubView.webView.estimatedProgress animated:YES];

            if(self.currentSubView.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
        {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }else if ([keyPath isEqualToString:@"title"]){
        if (object == self.webView) {
            self.title = self.webView.title;
        }else{
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }
}

相关文章

网友评论

    本文标题:iOS WKWebView 获得title和进度

    本文链接:https://www.haomeiwen.com/subject/eyzyqxtx.html