【发布时间】:2016-02-14 21:59:39
【问题描述】:
我使用WKWebView,我希望在网站完全加载时收到通知。当document.readyState 为interactive 或complete 时,WKNavigationDelegate 的webView:didFinishNavigation 方法被触发,我想确保该站点已完全加载。我想出了使用 JavaScript 注入的解决方案。这是我的 MWE:
import UIKit
import WebKit
class ViewController: UIViewController, WKScriptMessageHandler, WKNavigationDelegate {
var webView: WKWebView!
@IBOutlet weak var loadLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let contentController = WKUserContentController()
let scriptPath = NSBundle.mainBundle().pathForResource("script", ofType: "js")!
let scriptString = try! String(contentsOfFile: scriptPath)
let script = WKUserScript(source: scriptString, injectionTime: .AtDocumentStart, forMainFrameOnly: true)
contentController.addUserScript(script)
contentController.addScriptMessageHandler(self, name: "readyHandler")
let configuration = WKWebViewConfiguration()
configuration.userContentController = contentController
webView = WKWebView(frame: CGRect.zero, configuration: configuration)
webView.navigationDelegate = self
loadLabel.text = nil
}
@IBAction func loadWebsite() {
webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://stackoverflow.com")!))
loadLabel.text = "Loading..."
}
func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
print("message received")
loadLabel.text = "Complete"
}
}
这是script.js文件的内容:
document.onreadystatechange = function () {
if(document.readyState === "complete"){
webkit.messageHandlers.readyHandler.postMessage("");
}
}
userContentController:didReceiveScriptMessage 方法总是在 iOS 模拟器上调用,但在实际设备上(在我的情况下是 iPhone 6)上,大多数时候都不会调用它。知道它可能有什么问题,或者检查网站是否完全加载的另一种方法是什么?
【问题讨论】:
-
.AtDocumentEnd工作吗? -
@soflare 在模拟器上,是的,在设备上,不是。更有趣的是,我在另一台设备(带有 iOS 8.4.1 的 iPhone 5)上对其进行了测试,它运行良好。所以这看起来像是 iOS 9 本身的问题。
-
确保: 1. 脚本被注入; 2.脚本执行; 3. 消息送达。哪一步坏了?尝试使用 Safari 附加 webview 并在 JS 控制台中检查。
标签: ios swift webkit wkwebview