gpt4 book ai didi

ios - 如何在 Swift 中监控 WKWebView 页面加载进度?

转载 作者:可可西里 更新时间:2023-11-01 04:24:19 29 4
gpt4 key购买 nike

这是我的代码:

@IBOutlet weak var webView: WKWebView!
@IBOutlet weak var progressView: UIProgressView!

override func viewDidLoad() {
super.viewDidLoad()

webView.uiDelegate = self
webView.navigationDelegate = self

webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if (keyPath == "estimatedProgress") {
progressView.setProgress(Float(webView.estimatedProgress), animated: true)
print(webView.estimatedProgress)
}
}

但 estimatedProgress 仅显示两个 float (0.1 和 1.0),我认为它不起作用。我使用了 Alamofire 进度,它以毫秒为单位进行更改,这使我的 UI 变得更好,但是这段代码无法正常工作...

有人可以帮助我了解 webView 的进展吗?

最佳答案

swift 4.0

@IBOutlet weak var webView: WKWebView!
@IBOutlet weak var progressView: UIProgressView!
override func viewDidLoad() {
super.viewDidLoad()

// load website or HTML page
self.webView.load(NSURLRequest(url: URL(string: "https://www.apple.com")!) as URLRequest);

//add observer to get estimated progress value
self.webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil);
}

// Observe value
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "estimatedProgress" {
print(self.webView.estimatedProgress);
self.progressView.progress = Float(self.webView.estimatedProgress);
}
}

关于ios - 如何在 Swift 中监控 WKWebView 页面加载进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47988125/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com