13

I am developing a Safari Extension for my local Mac App, I want to use SafariExtension Companion to communicate between them.

However, in my Global.html of the extension, whenever I am trying to get safari.extension.companion, it is always null. Do I setup the extension wrong?

Besides, according to the guide, I can use safari.extension.addEventListener to listen the message from local app, however, when I debug the extension, it prompts me safari.extension.addEventListener is undefined, why is that?

Answer: safari.application.addEventListener is correct, Apple write a wrong document.

I do setup the exact Companion Bundle Identifier in Extension Builder as the identifier which I set in Xcode for the safari companion according to the guide. Is there anything else I miss? Please help!!!

Terence
  • 443
  • 3
  • 13

1 Answers1

5

A Safari extension companion is a type of a MacOS X app extension. Those are shipped as part of application bundles. When the app is installed (the bundle is copied to Applications), Safari picks up the extension.

First, you need an Xcode project with a target of type "Cocoa application" ("Command line tool" won't do). Then you need to create a target of type "Safari Extension Companion", and you have a chance to specify the primary app while creating it.

When you build and archive the app, and export the archive, you get an option to add the extension to the app bundle. Then install the app bundle.

Make sure the bundle ID of the extension matches the setting in the Safari Extension Builder. Also, make sure the app and the Safari extension are both signed, and the keys have matching developer IDs. For certificate type on MacOS X, choose "Developer ID" - first when requesting, then when exporting the bundle from the build archive.

EDIT: when the native companion crashes, Safari quietly disables it. If you remove and reinstall the app bundle, and also restart Safari, it loves the companion again.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • Thanks for replying. I did all the things, but it still doesn't work. What I did: 1.I import Safari Certificate in the extension. 2. Sign the app and companion with Mac Development Certificate with the same prefix 3. Archive it and export it as Development-Signed Application and then copy the bundle to Application folder – Terence Feb 03 '16 at 15:39
  • The native part should be signed, too. I'm not sure if you need to sign the main app, the extension, or both, so I've signed both when building **and** when exporting. The certificates for Safari and MacOS X are physically distinct, but they have the same developer ID in them; in my experience, that's sufficient. – Seva Alekseyev Feb 03 '16 at 15:42
  • Does it has to be signed with a Mac Distribution Certificate? I only have Development Certificate. – Terence Feb 03 '16 at 15:54
  • When requesting a MacOS X certificate at the Apple site, I've specified "Developer ID" for type. Despite the same, it's a distribution (i. e. production) certificate. When exporting the bundle from the archive, I'd also specify "Export a Developer ID-signed application". – Seva Alekseyev Feb 03 '16 at 16:10
  • It just works for a while... When Safari crashed and I open it again, it does not work. From the log I can't see anything special but only one log. In the successful case, there is a message "pkd: enabling pid = xxxxx for plugin-in (plugin name, address and path)". And now this message is never showing again. I suspect that if it is related to some settings in Safari? – Terence Feb 03 '16 at 19:18
  • Noticed that. Looks like once the plugin crashes, Safari quietly disables it. If you remove and reinstall the app bundle, and also restart Safari, it loves the plugin again. – Seva Alekseyev Feb 03 '16 at 19:50
  • I reinstalled it without restarting Safari, that's why....Thank you. Problem Solved! – Terence Feb 03 '16 at 19:59