如何使用 WKScriptMessageHandler 读取发送到父元素的邮件?
How to read a postmessage sent to a parent element using WKScriptMessageHandler?
我正在 WKWebView 中加载 URL,它正在向父元素发送邮寄消息:
window.parent.test.postMessage('A message')
我正在实施 WKScriptMessageHandler 并尝试使用 userController(didReceiveMessage):
获取该消息
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if(message.name == "test") {
print("JavaScript is sending a message \(message.body)")
}
}
message.body有点像
{
command = "015.1";
}
我想将这个值015.1读入一个字符串变量
首先应该将消息作为字典传递
window.parent.test.postMessage({"command":id})
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
print("chegou mensagem do javascript ")
print(message.body)
print(message.name)
let dict = message.body as? NSDictionary;
let str = dict?["command"] as? String
print(str ?? "")
我正在 WKWebView 中加载 URL,它正在向父元素发送邮寄消息:
window.parent.test.postMessage('A message')
我正在实施 WKScriptMessageHandler 并尝试使用 userController(didReceiveMessage):
获取该消息func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if(message.name == "test") {
print("JavaScript is sending a message \(message.body)")
}
}
message.body有点像
{
command = "015.1";
}
我想将这个值015.1读入一个字符串变量
首先应该将消息作为字典传递
window.parent.test.postMessage({"command":id})
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
print("chegou mensagem do javascript ")
print(message.body)
print(message.name)
let dict = message.body as? NSDictionary;
let str = dict?["command"] as? String
print(str ?? "")