我正在测试即将发布的 iOS 11 的应用程序。在使用应用程序内浏览器 (WKWebView) 导航时,我使用 KVO 来更新进度。不幸的是,它永远不会达到 100%。这是我的代码(在 iOS 10 中完美运行):
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "estimatedProgress" {
print("progres \(webView.estimatedProgress)")
if webView.estimatedProgress == 1.0 {
print("progress reached 100%")
progressView.setProgress(1.0, animated: true)
UIView.animate(withDuration: 0.3, delay: 0.3, options: .curveEaseOut, animations: {
self.progressView.alpha = 0.0
}, completion: { (finished) in
self.progressView.setProgress(0.0, animated: false)
self.progressView.isHidden = true
})
}
else {
progressView.alpha = 1.0
progressView.isHidden = false
progressView.setProgress(Float(webView.estimatedProgress), animated: true)
}
} else if keyPath == "title" {
navigationItem.title = webView.title
}
else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
}
以下是 Xcode 控制台的打印输出示例:
> progres 0.1
> progres 0.5
> progres 0.55430649853092
> progres 0.771510689002141
> progres 0.77167516785194
最佳答案
经过调查,问题似乎来 self 正在使用的特定内部 URL 的复杂网络设置。使用代理解决了这个问题。
关于iOS 11 - WKWebView 进度 KVO 从未达到 100%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46081629/