[英]Can I send message directly from Safari Web Extension’s background page to Native App?
I am developing a safari web extension.我正在开发一个 Safari 网络扩展。 I need send data from Web Extension's background page to native app.
我需要将数据从 Web Extension 的后台页面发送到本机应用程序。 Native app gets this message and signs it with USB key to generate a signature value.
本机应用程序获取此消息并使用 USB 密钥对其进行签名以生成签名值。 Then Native app sends the signature value back to background page.
然后本机应用程序将签名值发送回后台页面。 Messaging a Web Extension's Native App
向 Web 扩展的本机应用程序发送消息
First, I create a port in background.js首先,我在 background.js 中创建一个端口
let port = browser.runtime.connectNative("application.id");
Add a listener to that port to receive message from Native App,as follow:在该端口上添加一个监听器以接收来自 Native App 的消息,如下:
port.onMessage.addListener(function(message) {
console.log("Received native port message:");
console.log(message);
});
In my native app, I can send message to background page with following codes:在我的本机应用程序中,我可以使用以下代码向后台页面发送消息:
SFSafariApplication.dispatchMessage(withName: "Hello from App",
toExtensionWithIdentifier: extensionBundleIdentifier,
userInfo: ["AdditionalInformation": "Goes Here"],
completionHandler: { (error) -> Void in
os_log(.default, "Dispatching message to the extension finished")
})
But how can I send message from background page to Native App?但是如何从后台页面向 Native App 发送消息? In “Messaging a Web Extension's Native App” demo, messages are send by following code:
在“Messaging a Web Extension's Native App”演示中,消息通过以下代码发送:
port.postMessage("Hello from JavaScript Port");
But there are no codes to show how to receive this message in native app.但是没有代码显示如何在本机应用程序中接收此消息。
In native app, How can I receive messages sent by “postMessage” from background page?在原生应用程序中,如何从后台页面接收“postMessage”发送的消息? If you can provide a demo with object-C, I will be very grateful.
如果能提供object-C的demo,将不胜感激。 Thanks!
谢谢!
Found the following answer to your question in the WWDC'20 session Meet Safari Web Extensions
在 WWDC'20 session
Meet Safari Web Extensions
中找到了您的问题的以下答案
App ⭤ extension :
应用⭤扩展:
Shared NSUserDefaults from an app group, or NSXPCConnection
来自应用组或 NSXPCConnection 的共享 NSUserDefaults
See also WWDC NOTES另请参阅WWDC 注释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.