2017-08-23 16 views
0

私はあなたのウェブサイトに表示されるものを制御するために角度を使用するjavascriptを使用するウェブサイトを持っています。したがってhttp://somewebsite.com/#page1が表示され、場所がhttp://somewebsite.com/#page2に変更されたタブをクリックすると、ウェブサイトには実際にリロードされないため、func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)はトリガーされません。どのようにこれらのハッシュ変更をキャッチすることができますか?WKWebViewのハッシュ変更を検出する方法は?

Safariウェブブラウザがロケーションバーにこれらの変更を表示できるので、明らかにAppleはできます。


更新19.33:

私はいくつかの研究をしました。 Safariブラウザでwindow.onhashchangeを使用すると、WKWebViewでは起動されませんが、起動しません。私は次のjavascriptを使用して毎秒ドキュメントの位置hrefを返します。

window.setInterval(function(){ alert(document.location) }, 1000); 

WKWebViewでそのアラートを読んSafariであなたがhttp://somewebsite.com/#page1http://somewebsite.com/#page2を参照しながら、それは、その場所の変化を示していることはありません。 WKWebViewは非常に迷惑なこの機能をサポートしていないようです。

答えて

0
let script = WKUserScript(source: "(function() { \"use strict\"; addEventListener(\"hashchange\", function(event) { webkit.messageHandlers.hashchangeMessageHandler.postMessage(null); }); })();", injectionTime: WKUserScriptInjectionTime.atDocumentEnd, forMainFrameOnly: false) 

let configuration = WKWebViewConfiguration() 
configuration.userContentController.add(self, name: "hashchangeMessageHandler") 
configuration.userContentController.addUserScript(script) 

self.webView = WKWebView(frame: CGRect.zero, configuration: configuration) 

今、あなたはハッシュを取得することができますがでURLを変更:

func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { 
    print("Received message: \(message.name): \(message.body)") 
    print(self.webView.url) 

} 
関連する問題