WkWebKit - javascript on loaded page finds window.webkit is undefined

后端 未结 4 1862
别跟我提以往
别跟我提以往 2020-12-31 06:46

I\'m experimenting with WkWebKit talking back and forth between app and page. I can get javaScript to execute fine using WkWebView evaluateJavascript method, but when I try

4条回答
  •  执念已碎
    2020-12-31 06:58

    I solved it and problem was that if you set userContentController to a new object, that userContentController's WKScriptMessageHandlers will not be registered correctly in Apple's internal code:

    WKUserContentController *userContentController = [WKUserContentController new];
    userContentController.addScriptMessageHandler(self, name: "jockey")
    userContentController.addScriptMessageHandler(self, name: "observe")
    webView.configuration.userContentController = userContentController
    

    fixed by using the already instantiated userContentController by Apple:

    webView.configuration.userContentController.addScriptMessageHandler(self, name: "jockey")
    webView.configuration.userContentController.addScriptMessageHandler(self, name: "observe")
    

提交回复
热议问题