简体   繁体   English

我可以直接从 Safari Web Extension 的后台页面向 Native App 发送消息吗?

[英]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.

相关问题 从电子设备加载Safari App Extension时,该如何调试? - How can I debug my Safari App Extension when I load it from electron? 如何在 safari 应用程序和从 safari 应用程序扩展资源文件夹加载的 html 页面之间进行通信 - how to communicate between safari app and html page loaded from safari app extension resources folder 无法在 Safari Web Extension Native App 中创建出站 unix 域套接字 - Unable to create outbound unix domain socket in Safari Web Extension Native App 如何以编程方式关闭Safari App Extension popover? - How can I close a Safari App Extension popover programmatically? 如何从命令行构建Safari扩展包? - How can I build a Safari extension package from the command line? iOS和OS X上的本机应用程序可以写入和读取Safari的网站数据(例如Cookie和Web存储)吗? - Can native apps on iOS and OS X write and read Safari's website data like cookies and Web Storage? 如何通过 safari web 扩展打开本地应用程序? - how to open local app through safari web extension? 我可以使Safari App Extension工具栏按钮的文本徽章变得不太难看吗? - Can I make text badges for Safari App Extension toolbar buttons less ugly? 一旦我进行任何修改,从 Safari 注入的 Javascript App Extension 将停止工作 - Injected Javascript from Safari App Extension stops working once I make any modification 未安装Safari App Extension - Safari App Extension not installed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM