2

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!

John
  • 41
  • 5

1 Answers1

0

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

ricobeck
  • 172
  • 1
  • 11