WKUserScript 在 iOS10 中不调用,但在 iOS9 中工作



我的代码在iOS 9中完美运行,但在iOS 10中不起作用,特别是未调用doBar((。在WKWebView中,我正在注入javascript代码。

let jsFoo = "function doFoo() { window.webkit.messageHandlers.doFoo.postMessage("doFoo"); }"
let jsBar = "class MyInterface { static doBar() { window.webkit.messageHandlers.doBar.postMessage("doBar"); } }"
let fooScript = WKUserScript(source: jsFoo, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
let barScript = WKUserScript(source: jsBar, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
contentController.addUserScript(logoutScript)
contentController.addUserScript(openPDFScript)
contentController.add(self, name: "doFoo")
contentController.add(self, name: "doBar")

在网页中 js 代码进行调用:

window.doFoo() // works in both iOS 9 and iOS 10
window.MyInterface.doBar() // works in iOS 9, but NOT working in iOS 10

Safari调试器在iOS 10窗口中显示。MyInterface 未定义,但存在带有 doBar 代码的用户脚本。

我怎样才能正确地注入 doBar,这样它就可以在 iOS 10 中工作,假设我无法更改 Web 代码?

好吧,因为我不是JS开发人员,我认为注入的代码很好。但它不是针对iOS 10 jsBar必须:

let jsBar = "function MyInterface() {}; { MyInterface.doBar = function() { window.webkit.messageHandlers.doBar.postMessage("doBar"); };"

相关内容

最新更新