1

I have a viewController in which I created a WKWebView, here is one of the delegate methods:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

    let model = SwiftJSModel()

    self.jsContext = webView.value(forKeyPath: "documentView.webView.mainFrame.javaScriptContext") as! JSContext

    model.jsContext = self.jsContext

    self.jsContext.setObject(model, forKeyedSubscript: "modelObject" as NSCopying & NSObjectProtocol)

}

however, my app will crash after this webView has been loaded, the console shows:

reason: '[<WKWebView 0x7fc41d9c7200> valueForUndefinedKey:]: this class is not key value coding-compliant for the key documentView.'

It seems that webView.value(forKeyPath: "documentView.webView.mainFrame.javaScriptContext") doesn't work in swift3, does somebody know why?

Jeremy
  • 91
  • 3
  • 12
  • Not the same reason, I created this viewController in code entirely. – Jeremy Jun 07 '17 at 10:38
  • if you comment this code block, did your code works well? – MGY Jun 07 '17 at 10:40
  • Yes, it worked well. And if i debug step by step, it will crash when meeting this line ```webView.value(forKeyPath: "documentView.webView.mainFrame.javaScriptContext") as! JSContext```. – Jeremy Jun 07 '17 at 11:13

1 Answers1

1

It seems that WKWebView cannot get the JSContext, here is where I found it: How to get JSContext from WKWebView

Jeremy
  • 91
  • 3
  • 12