如何通过WKWebView更改AMP页面的URL



我正在使用WKWebView开发简单的Web浏览器。我可以检测到何时在Spa上更改URL,例如Trello,并使用自定义JavaScript更改。

这样,在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"one_answers" ### ep"的新组合。

最新更新