简体   繁体   中英

Can I send message directly from Safari Web Extension’s background page to Native App?

I am developing a safari web extension. I need send data from Web Extension's background page to native app. Native app gets this message and signs it with USB key to generate a signature value. Then Native app sends the signature value back to background page. Messaging a Web Extension's Native App

First, I create a port in background.js

let port = browser.runtime.connectNative("application.id");

Add a listener to that port to receive message from Native App,as follow:

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? In “Messaging a Web Extension's Native App” demo, messages are send by following code:

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? If you can provide a demo with object-C, I will be very grateful. Thanks!

Found the following answer to your question in the WWDC'20 session Meet Safari Web Extensions

App ⭤ extension :

Shared NSUserDefaults from an app group, or NSXPCConnection

See also WWDC NOTES

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM