When sending a message through Javascript to iOS, I encountered this error message that I can't find any information online: "Can only call UserMessageHandler.postMessage on instances of UserMessageHandler".
I am using a webview to render a webpage on iOS.
I tried adding the same script from native code and I was able to receive the message. However, the same script shows the above error if I deploy it on the web site.
let scriptSource = "window.webkit.messageHandlers.jsHandler.postMessage({command: 'command goes here'});"
let userScript = WKUserScript(source: scriptSource, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
userContentController.addUserScript(userScript)
on the web end, I used the following code
key: "onExit",
value: function() {
var t = function() {
try {
return window.webkit.messageHandlers.jsHandler.postMessage || null
} catch (t) {
return null
}
}();
if (t)
try {
t({
command: "command goes here"
}), console.log("window.webkit.messageHandlers.jsHandler.postMessage called successfully")
} catch (t) {
console.log("error thrown when calling window.webkit.messageHandlers.jsHandler.postMessage - " + (t || {}).message)
}
else
console.log("window.webkit.messageHandlers.jsHandler.postMessage not found!")
}