0
我使用的代碼在iOS 9中完美工作,但不在iOS 10中工作,特別是未調用doBar()。在WKWebView中,我正在注入JavaScript代碼。WKUserScript不能在iOS10中調用,但在iOS9中工作
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中工作,假設我的代碼不能更改?