title: Swift and Javascript Integration
body: <p>i was following along <a href="http://www.kinderas.com/technology/2014/6/15/wkwebview-and-javascript-in-ios-8-using-swift" rel="nofollow">this article</a> about integrating <strong>Swift (WKWebView) and Javascript.</strong> Everything from the article works fine but i am trying to pass some variable from Swift to Javascript.</p>
  
  <p>This is my ViewController in Swift:</p>
  
  <pre><code>import UIKit
  import WebKit
  
  class ViewController: UIViewController, WKScriptMessageHandler {
  
      @IBOutlet var containerView : UIView! = nil
      var webView: WKWebView?
  
      override func loadView() {
          super.loadView()
  
          let contentController = WKUserContentController();
          let userScript = WKUserScript(
              source: "redHeader()",
              injectionTime: WKUserScriptInjectionTime.AtDocumentEnd,
              forMainFrameOnly: true
          )
          contentController.addUserScript(userScript)
          contentController.addScriptMessageHandler(
              self,
              name: "callbackHandler"
          )
  
          let config = WKWebViewConfiguration()
          config.userContentController = contentController
  
          self.webView = WKWebView(
              frame: self.view.frame,
              configuration: config
          )
          self.view = self.webView!
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          let url = NSURL(string:"https://example.com")
          let req = NSURLRequest(URL:url!)
          self.webView!.loadRequest(req)
  
          let simpleInt = 123
  
          self.webView?.evaluateJavaScript("redHeader(\(simpleInt)", completionHandler: nil);
      }
  
      func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
          if(message.name == "callbackHandler") {
              print("JavaScript is sending a message \(message.body)")
          }
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
      }
  }
  </code></pre>
  
  <p>and thats the <strong>HTML and JS</strong> residing on an server:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>function callNativeApp () {
      try {
          webkit.messageHandlers.callbackHandler.postMessage("Hello from JavaScript");
      } catch(err) {
          console.log('The native context does not exist yet');
      }
  }
  
  setTimeout(function () {
      callNativeApp();
  }, 50000);
  
  function redHeader(int) {
   console.log(int);
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;style type="text/css"&gt;
              body {
                  padding-top: 40px; 
              }
          &lt;/style&gt;
          &lt;title&gt;WKWebView Demo&lt;/title&gt;
          &lt;meta charset="UTF-8"&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;WKWebView Test&lt;/h1&gt;
          &lt;script type="text/javascript" src="main.js"&gt;&lt;/script&gt;
          &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"&gt;&lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>It's still just the code from the above article but i am not able to pass the variable <strong>simpleInt</strong> to the JS. </p>
  
  <p>Despite that the Javascript call</p>
  
  <pre><code>webkit.messageHandlers.callbackHandler.postMessage("Hello from JavaScript");
  </code></pre>
  
  <p>does not work as well.</p>
  
  <p>I think I am passing the variable wrong. Any suggestions?</p>