WKUserScript 未在 iOS10 中调用,但在 iOS9 中有效

WKUserScript not call in iOS10, but works in 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 window.MyInterface 未定义,但存在带有 doBar 代码的用户脚本。

如何正确注入 doBar,使其在 iOS 10 中工作,假设我无法更改 Web 代码?

虽然我不是 JS 开发人员,但我认为注入的代码很好。但它不适用于 iOS 10 jsBar 必须是:

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