Add, build, and enable a Safari App Extension.
You deliver a Safari App Extension inside a macOS app. When a user runs your app for the first time, the app extension immediately becomes available in Safari. This article walks you through the following steps to build your own Safari App Extension:
Adding a Safari App Extension target to your app in Xcode.
Adding script and style sheet files to inject into your app and modify webpage content.
Adjusting property list values in your app that relate to the Safari App Extension.
Building and running your application, which builds your Safari App Extension for use.
Enabling your Safari App Extension in Safari so you can use it.
Add a Safari App Extension Target in Xcode
To create a Safari App Extension, add a new target to an existing macOS app project in Xcode:
Launch Xcode and either open an existing project containing a macOS app or create a new one.
Choose File > New > Target.
From the list of templates in the Application Extension section, select Safari Extension, and click Next.
Enter a product name for your extension, such as “My Extension.”
Make sure that your application project is selected in the Project menu, and that your macOS application target is selected in the Embed in Application menu.
When Xcode asks you if you want to activate a new scheme for your new extension, click Cancel.
Xcode adds a new group into your project, which contains several new files, including an
Add Scripts and Style Sheets
After you add scripts and style sheets to your Safari App Extension, you'll be able to read and modify webpage content, and read and send data from your app. For more about injecting scripts and style sheets into your webpage content, see About Injected Style Sheets and Scripts.
Add Keys to the Info Property List Template
Info file identifies your new Safari App Extension and specifies its capabilities and access limits under the
NSExtension key. For more about setting these capabilities and limits, see Safari App Extension Info Property List Keys. In your info property list file, you can also specify custom UI options, like contextual menu items and toolbar items. For more about these settings, see About Contextual Menu and Toolbar Item Keys.
Build and Run the Application
Before Safari can find your new extension, you must run the parent application at least once:
Make sure that your macOS app is selected in the Scheme menu next to the Run and Stop buttons in Xcode’s main toolbar.
Click the Run button, or choose Product > Run (Command-R) to build and run your app.
When you build your app, Xcode builds your Safari App Extension first, then embeds it inside the finished app bundle. As soon as your app runs, your extension is ready for use in Safari.
Enable Your App Extension in Safari
If you’re not part of the Apple Development Program, or if you haven’t yet configured a developer identity for your existing Xcode project, your Safari App Extension won’t be signed with a development certificate. For security purposes, Safari, by default, ignores unsigned extensions, so your extension won’t show up in Safari Extensions preferences.
To develop without a certificate, each time Safari is launched, you must tell it to load unsigned extensions using the Develop menu:
Open Safari and choose Safari > Preferences.
Select the Advanced tab, then select the “Show Develop menu in menu bar” checkbox.
Choose Develop > Allow Unsigned Extensions. The Allow Unsigned Extensions setting is reset when a user quits Safari; you must set it again the next time Safari is launched.
Select the Extensions tab. This tab shows the localized description, display name, and version number for the selected Safari App Extension. It also provides a more nuanced message about the permissions claimed by the extension.
Find your new extension in the list on the left, and enable it by selecting the checkbox.
Close Safari Preferences.
Your new extension is now enabled. If you built the default template extension without modifying it, you’ll see a new toolbar item (a button) in the Safari toolbar. When you press this button, the Safari App Extension logs a message that you can view in the System Console—this message comes from your
If you have trouble getting your extension to work, see Troubleshooting Your Safari App Extension.