title: Injecting global object in WKWebView
body: <p>I'm attempting to add a global object to a WKWebView where the loaded HTML expects a certain object to be present (with an API). I have the following setup to prime the HTML after loading. This works in this following trivial example, but with the 'real' content loaded with loadRequest it fails with "[Error] ReferenceError: Can't find variable". Anyone have a similar experience?</p>
  
  <pre><code>@implementation ViewController
  
  - (void)viewDidLoad {
      [super viewDidLoad];
      [self setupWebView];
  }
  
  - (void)setupWebView
  {
      WKWebViewConfiguration *webConfiguration = [[WKWebViewConfiguration alloc] init];
      WKUserContentController *userContentController = [[WKUserContentController alloc] init];
  
      NSString* jsCode =
      @"var TheObject = function() {}; \
      TheObject.prototype.sendTheMessage = function() { \
        window.webkit.messageHandlers.performAction.postMessage(messageToPost); \
      }; \
      var theObject = new TheObject();";
  
      WKUserScript *userScript = [[WKUserScript alloc] initWithSource:jsCode
                                                        injectionTime:WKUserScriptInjectionTimeAtDocumentStart
                                                     forMainFrameOnly:NO];
      [userContentController addUserScript:userScript];
      [userContentController addScriptMessageHandler:self
                                                name:@"performAction"];
      webConfiguration.userContentController = userContentController;
  
      self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds
                                        configuration:webConfiguration];
      [self.view addSubview:self.webView];
  
      [self.webView loadHTMLString:
       @"&lt;body onload='sendMessage()'&gt; \
       &lt;script&gt;function sendMessage() { \
         theObject.sendTheMessage(); \
       } \
       &lt;/script&gt; \
       Attempting to send the message \
       &lt;/body&gt;" baseURL:nil];
  }
  
  - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
  {
      NSLog(@"%@", message);
  }
  
  @end
  </code></pre>