WKUserScript not call in iOS10, but works in iOS9

761 Views Asked by At

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?

1

There are 1 best solutions below

0
Jurasic On BEST ANSWER

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\"); };"