title: Expose native methods in JavaScript in WKWebView
body: <p>I work on a native Objective-C library and I'm exposing some of the API into JavaScript. The purpose is to make it accessible on both Android and iOS</p>
  
  <p>On iOS, I use JSContext and JSExport <a href="https://developer.apple.com/reference/javascriptcore/jsexport" rel="nofollow">as described here</a></p>
  
  <p><strong>Objective-C</strong></p>
  
  <pre><code> @import JavaScriptCore;
  
  
  @protocol PersonExports &lt;JSExport&gt;
  
  + (void)sayHello:(NSString *)name;
  
  @end
  
  @interface Person : NSObject
  
  + (void)sayHello:(NSString *)name;
  
  @end
  </code></pre>
  
  <p>Then I add the exposed object to the JSContext of a <code>UIWebView</code> when it finishes loading:</p>
  
  <pre><code>JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
  
  if (nil != context) {
      context[@"Person"] = [Person class];
  }
  </code></pre>
  
  <p>After this, I can access the object from the JavaScript in the loaded page and call the method on it:</p>
  
  <p><strong>JavaScript</strong></p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  Person.sayHello(@"Mickey");
  
  &lt;/script&gt;
  </code></pre>
  
  <p>The question is how can I achieve this in a <code>WKWebView</code>?</p>