Webview & Javsscript

from javascript to ios

javascriptでiosのfunctionをwebkit.messageHandlersでcall

// window.webkit.messageHandlers.[name].postMessage // syntax
window.webkit.messageHandlers.callbackHandler.postMessage('Hello Native!');

ios側で callbackHandler部分を処理するコードを作成

func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
  if(message.name == "callbackHandler") {
    print("JavaScript is sending a message : \(message.body)")
  }
}
  • [name]は whatever you call the script handler

from ios to javascript

iosからjavascriptのfunctionを呼び出し

References

results matching ""

    No results matching ""