如何检测 amp 页面的 url 何时随 WKWebview 更改
How can I detect when url of amp page changed with WKWebview
我正在使用 WKWebview 开发简单的网络浏览器。
我可以检测到 url 何时在 SPA 中更改,例如带有自定义 Javascript.
的 trello
这种方式在 amp 页面中不起作用。 (Google 的加速移动页面)
我尝试将 print("webView.url")
放入所有 WKNavigationDelegate
函数
但是我无法检测到 amp page url
.
的变化
但是 webView
有 amp 页面 url ,我想将 amp 页面 url 保存到本地商店。
同样的问题。不幸的是,WKWebView 仅在整个页面加载发生时才触发其功能。
所以我们要做的是在 WebKit.url 属性.
上使用键值观察
看起来像这样:
import AVFoundation
import UIKit
import WebKit
import MediaPlayer
class ViewController: UIViewController, WKNavigationDelegate {
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
self.webView.addObserver(self, forKeyPath: "URL", options: .new, context: nil)
self.webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
self.webView.load(URLRequest(url: "https://google.com"))
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == #keyPath(WKWebView.url) {
print("### URL:", self.webView.url!)
}
if keyPath == #keyPath(WKWebView.estimatedProgress) {
// When page load finishes. Should work on each page reload.
if (self.webView.estimatedProgress == 1) {
print("### EP:", self.webView.estimatedProgress)
}
}
}
wkWebkitView 中的每个额外导航都应该触发“### URL”和“### EP”的新组合。
我正在使用 WKWebview 开发简单的网络浏览器。 我可以检测到 url 何时在 SPA 中更改,例如带有自定义 Javascript.
的 trello这种方式在 amp 页面中不起作用。 (Google 的加速移动页面)
我尝试将 print("webView.url")
放入所有 WKNavigationDelegate
函数
但是我无法检测到 amp page url
.
但是 webView
有 amp 页面 url ,我想将 amp 页面 url 保存到本地商店。
同样的问题。不幸的是,WKWebView 仅在整个页面加载发生时才触发其功能。
所以我们要做的是在 WebKit.url 属性.
上使用键值观察看起来像这样:
import AVFoundation
import UIKit
import WebKit
import MediaPlayer
class ViewController: UIViewController, WKNavigationDelegate {
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
self.webView.addObserver(self, forKeyPath: "URL", options: .new, context: nil)
self.webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
self.webView.load(URLRequest(url: "https://google.com"))
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == #keyPath(WKWebView.url) {
print("### URL:", self.webView.url!)
}
if keyPath == #keyPath(WKWebView.estimatedProgress) {
// When page load finishes. Should work on each page reload.
if (self.webView.estimatedProgress == 1) {
print("### EP:", self.webView.estimatedProgress)
}
}
}
wkWebkitView 中的每个额外导航都应该触发“### URL”和“### EP”的新组合。