常用的方法,WKWebView的代理方法:
//执行JS方法获取导航栏标题
[webView evaluateJavaScript:@"document.title" completionHandler:^(id _Nullable title, NSError * _Nullable error) {
if (ValidStr(title)) {
self.title = title;
} else {
self.title = @"";
}
}];
2.在第一种方法获取不到title的时候,添加KVO监听方法,用来获取webView的title
// 添加进度监听
[_webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
#pragma mark KVO 实现方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"title"]) {
if (object == self.webView) {
self.title = self.webView.title;
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
}