title: How to pass data from swift to javascript?
body: <p>I know how to pass data from javascript to swift, but don't know how to pass data from swift to javascript.</p>
  
  <p>The method i use to pass data from javascript to swift is below:</p>
  
  <p></p>
  
  <pre><code>&lt;head&gt;
  
      &lt;title&gt;Test&lt;/title&gt;
      &lt;meta charset="UTF-8"&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h1&gt;WebView Test 3&lt;/h1&gt;
      &lt;script&gt;
          function callNativeApp () {
              try {
                  webkit.messageHandlers.callbackHandler.postMessage("Here");
              } catch(err) {
                  console.log('The native context does not exist yet');
              }
          }
      callNativeApp();
          &lt;/script&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>import UIKit
  
  import WebKit
  
  class ThirdViewController: UIViewController,WKScriptMessageHandler{
  
  
      override func viewDidLoad() {
  
          super.viewDidLoad()
  
          let configuration=WKWebViewConfiguration()
  
          let controller=WKUserContentController()
  
  
          controller.addScriptMessageHandler(self, name: "callbackHandler")
  
  
          configuration.userContentController=controller
  
          let webView=WKWebView(frame: self.view.frame, configuration: configuration)
  
          let url=NSBundle.mainBundle().URLForResource("test3", withExtension: "html")
  
          let request=NSURLRequest(URL: url!)
  
          self.view=webView
  
          webView.loadRequest(request)
      }
  
      func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
  
          if(message.name == "callbackHandler"){
              print("callbackHandler: \(message.body)")
          }
  
  
  
      }
  
  
  }
  </code></pre>