本次使用的目的是监听 UIWebView 的变化, 但是监听值不能直接取出, 需要as 转换
1.Swift 的change 不能直接取出
2.eroor 取值
// 添加KVO 监听
self.wkWebView.addObserver(self, forKeyPath: "title", options: NSKeyValueObservingOptions.new, context: nil)
// MARK: - KVO 监听 取值: as 转换类型后, 和 OC 一样的操作
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
let changeDic = change! as NSDictionary
let title:String = changeDic.value(forKey: "new")! as! String
print("当前的 change", changeDic.value(forKey: "new")!)
let js = "callJsAlert()"
self.wkWebView.evaluateJavaScript(js) { (re, error) in
let er = error! as NSError
let url = er.userInfo["WKJavaScriptExceptionSourceURL"]
print("当前的 URL", url!)
if title.contains("监听的属性变化值") {
// 出发事件
}
}
}