title: Cannot read property 'messageHandlers' of undefined
body: <p>I want to pass JavaScript variable to Swift.
  I get an error in JavaScript &amp; search for that but I didn't get any result.
  The error is:</p>
  
  <blockquote>
    <p>TypeError: Cannot read property 'messageHandlers' of undefined</p>
  </blockquote>
  
  <p>Any one can help?</p>
  
  <p>My code in Xcode:</p>
  
  <pre><code>import UIKit
  import WebKit
  
  class ViewController: UIViewController,WKScriptMessageHandler,WKNavigationDelegate {
  
      var webView = WKWebView()
      override func viewDidLoad() {
          super.viewDidLoad() 
  
          webView.frame = view.bounds
          webView.navigationDelegate = self
          let url = URL(string: "MyUrl")!
          let urlRequest = URLRequest(url: url)
          webView.load(urlRequest)
          webView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
          view.addSubview(webView)
          let userContentController = WKUserContentController()
          userContentController.add(self, name: "test")
      }
  
      func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
          print("Name: \(message.name)")
          print("Body: \(message.body as! String)")
     }
  }
  </code></pre>
  
  <p>And in JavaScript:</p>
  
  <pre><code>click: event =&gt; {
  window.webkit.messageHandlers.test.postMessage("hi");
  }
  </code></pre>