2017-07-15 75 views
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中工作,假設我的代碼不能更改?

回答

0

那麼因爲我不是JS開發人員,我認爲注入的代碼很好。但它不適用於iOS 10 jsBar必須是:

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