13
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WKWebViewでwindow.webkit.messageHandlersが効かないサイトへの対処法

Posted at

どういうわけかwindow.webkitをtrueで上書きしているサイトが少なからず存在します。
そのサイトにはWKWebViewのuserContentController:didReceiveScriptMessage:(WKScriptMessageHandler)を呼び出せません。

対処法

ページを読み込む前にwindow.webkitを別の変数に保存しておき、あとはその変数からmessageHandlersを使えばOKです。

こちらを参照: WKWebViewでJavaScriptを読み込み前後に仕込むサンプル

-------------------------
ページの読み込む前 JavaScriptを実行
MyWebkitNamespace = window.webkit;
-------------------------
ページの読み込み...
-------------------------
ページの読み込み後 JavaScriptを実行
MyWebkitNamespace.messageHandlers.myMessageTest.postMessage('ok');
-------------------------
13
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?