Safari Extension Companion is always null - xcode

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!!!

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.

Related

Code Signing an Automator Workflow file within macOS app bundle

I am attempting to upload a macOS binary to the Mac App Store via the Xcode Organizer.
I get an "Upload failed" error with the message:
App Store Connect Operation Error
ERROR ITMS-90276: Missing Bundle Identifier. The application bundle contains a tool or framework workflow [com.company.AppName.pkg/Payload/AppName.app/Contents/Resources/Resources/Scripts/workflow.workflow] that is missing the bundle identifier in its Info.plist file.
My app bundle contains AppleScript.scpt, ShellScript.sh, and workflow.workflow scripting files.
What kind of scripts need Info.plist and Bundle Identifiers for signing? How can I get past this error?
Apple requires that all bundled executables be code signed
What to Code Sign
You sign all the individual components of your app, leaving no gaps, including:
Nested code. First, you recursively sign all of the helpers, tools, libraries, frameworks, and other components that your app relies on, and that are bundled with your app.
https://developer.apple.com/library/archive/documentation/Security/Conceptual/CodeSigningGuide/Procedures/Procedures.html#//apple_ref/doc/uid/TP40005929-CH4-SW2
In this case, the workflow.workflow Automator Workflow bundle is failing code signature because it is missing a Bundle Identifier in its existing Info.plist.
I added the lines:
<key>CFBundleIdentifier</key>
<string>com.company.AppName.SampleWorkflow</string>
after adding the BundleID, the Xcode Organizer signs and uploads to the Mac App Store without issue.

Create a "Safari App Extension" for macOS application created with Electron?

Since the release of "Safari App Extension" I have toyed around with creating safari app extensions for native applications using xcode. Now however, I'm attempting to create an extension for an application that was built using the Electron Framework.
I'm wondering if this is possible and if anyone has had any luck accomplishing this goal. I'm unsure how to go about initiating the safari app extension without the use of xcode.
For those unfamiliar with "Safari App Extension" you can add one to an existing native project by opening a MacOS application, going to "File->New->Target->Safari Extension" this will then create the extension within your existing application and make the extension available within Safari->Preferences->Extensions.
Thank you for reading.
I'm not secure if what I'm saying it's true, but I think that Safari Extensions are writed with Safari apis and/or a language that it isn't javascript (because, on my old Mac, I needed to install an adblocker app with .app extension). The problem is that Electron is based by Chromium and requires javascript and Chromium based extensions. If you want, however, you can do the inverse.
If you are not expert with Chromium extensions, I suggest to you this link.
P.S.: You can also try to find the same or similar extension that works on Chromium.

Why can I run my signed Safari App Extension on other computers, but not the one I developed it on?

I’ve started a Safari App Extension project, following Apple’s documentation.
I’ve got my (empty) app, and my very minimal extension. I’ve archived the app, signing it with my Developer ID. When I copied the archived app to a friend’s Mac, ran it, and launched Safari, the app extension showed up in the Extensions pane of Safari’s preferences (with the parent app’s icon), and when enabled, worked fine.
However, when I run the app on my Mac (where I developed it), and launch Safari, the app extension does not show up in Safari’s Extensions preference pane unless I check “Allow Unsigned Exceptions” in Safari’s develop menu, as described in this answer — and when it does, it has a generic app icon, rather the parent app’s icon.
Although I hope to eventually make the app and the app extension available to other people, I’m mainly making it for myself. As such, I’d like to be able to use it without having to check “Allow Unsigned Exceptions” every time I start Safari.
How can I make the app extension show up in Safari’s Extensions preference pane on the computer I develop the extension on?
I found a debug version of my app and app extension in my user Library folder:
~/Library/Developer/XCode/DerivedData/{APP_NAME_FOLLOWED_BY_LONG_STRING}/Build/Products/Debug
After deleting the contents of this folder, restarting Safari, and allowing unsigned extensions again, my app extension no longer appeared with the generic icon in Safari’s Extensions preferences.
(I’m a real Xcode newbie, so I’ve no idea if deleting debug builds is a thing that I can/should do via Xcode instead.)
I then archived my app again and ran it, then restarted Safari. Now my app extension shows up in Safari’s Extensions preferences, with its proper icon and everything, and stays enabled between restarts.

Create OS X Today Extension without containing app

On the Apple Developer website it states:
To deliver an OS X app extension, it’s recommended that you submit
your containing app to the App Store, but it’s not required.
However, to make a Today Extension from what I understand: I must create a Cocoa OS X application then add a "Today Extension" as a target. However, I do not want/need this main containing app. All I need is the extension.
So, how do I create an extension without a containing app? Even though technically I need to develop the containing app to create an extension target.
I think you are misreading this: what they are saying is that an OS X app extension may be distributed OUTSIDE the app store - if you continue reading, you will see the following:
If you distribute an OS X app extension outside of the Mac App Store, Gatekeeper prevents the extension from running until the user opens and approves the containing app. Further, if you code sign with a certificate other than your Developer ID, users must explicitly override Gatekeeper to open the containing app to make your extension available.
All extensions, at this writing, must be part of a container app and Apple devotes a large number of pages describing how, why, etc. My understanding (and work on extensions) is that all extensions must be part of a containing application. If there is a way to do so without the container app, Apple has not indicated it.

OSX Today Extension without containing app

I'm not understanding if a containing app is needed alongside a Today Extension for OSX. I have a very simple and straightforward widget, it does not need nor rely on a main or containing app. With that said, how can I create an Today Extension without a containing app.
What I have so far is that I have a blank app with a today extension.
You cannot create a today extension without having an app. They don't work that way. All iOS app extensions are just that-- extensions to an app. There must be an app, and it must do something to be accepted into the app store (Apple rejects apps for "minimal functionality"). Extensions cannot exist on iOS outside of an app bundle, until or unless Apple decides to change things in the future.
A container app is not needed. Read up the Apple documentation on it. It says:
To deliver an OS X app extension, it’s recommended that you submit your containing app to the App Store, but it’s not required.

Resources