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

后端 未结 4 1868
别跟我提以往
别跟我提以往 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 07:03

    I ran into this SO because I was experiencing the same issue and this is how I solved it using a custom CustomContentController (subclass of WKUserContentController) instance.

    let userContentController = CustomContentController()
    let webViewConfiguration = WKWebViewConfiguration()
    webViewConfiguration.userContentController = userContentController
    webView = WKWebView(frame: .zero, configuration: webViewConfiguration)
    

    In my case the CustomContentController is a subclass of WKUserContentController in which the add(_ scriptMessageHandler: WKScriptMessageHandler, name: String) method is called, but I don't believe that is significant.

    I believe that the WKUserContentController must be instantiated and applied to a WKWebViewConfiguration before the WKWebView is initialized via WKWebView(frame: .zero, configuration: webViewConfiguration)

    If the WKWebView has been created and then you try change the WKWebViewConfiguration you will encounter window.webkit not being available in the JSContext.

提交回复
热议问题