title: Using JavaScript with WkWebView - error on UserMessageHandler
body: <p>When sending a message through Javascript to iOS, I encountered this error message that I can't find any information online: "Can only call UserMessageHandler.postMessage on instances of UserMessageHandler". </p>
  
  <p>I am using a webview to render a webpage on iOS. </p>
  
  <p>I tried adding the same script from native code and I was able to receive the message. However, the same script shows the above error if I deploy it on the web site. </p>
  
  <pre><code>let scriptSource = "window.webkit.messageHandlers.jsHandler.postMessage({command: 'command goes here'});"
  let userScript = WKUserScript(source: scriptSource, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
  userContentController.addUserScript(userScript)
  </code></pre>
  
  <p>on the web end, I used the following code</p>
  
  <pre><code>key: "onExit",
                      value: function() {
                          var t = function() {
                              try {
                                  return window.webkit.messageHandlers.jsHandler.postMessage || null
                              } catch (t) {
                                  return null
                              }
                          }();
                          if (t)
                              try {
                                  t({
                                      command: "command goes here"
                                  }), console.log("window.webkit.messageHandlers.jsHandler.postMessage called successfully")
                              } catch (t) {
                                  console.log("error thrown when calling window.webkit.messageHandlers.jsHandler.postMessage - " + (t || {}).message)
                              }
                          else
                              console.log("window.webkit.messageHandlers.jsHandler.postMessage not found!")
                      }
  </code></pre>