简体   繁体   中英

WKUserScript not call in iOS10, but works in iOS9

I'm having code which perfectly works in iOS 9, but not working in iOS 10, specifically doBar() not called. Here in WKWebView I'm injecting javascript code.

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")

In web page js code make calls:

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

Safari debugger shows that in iOS 10 window.MyInterface is undefined, however user-script with doBar code is present.

How can I inject doBar properly, so it will work in iOS 10, assuming that web code I can't change?

Well as I'm not JS developer I thought that injected code was fine. But it's not for iOS 10 jsBar have to be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM