Building Safari extensions for iPhone and Mac

Safari extensions run on iPhone, iPad and Mac from one project. What you need, how they're built, and what I learned making Hour.

2 min readby Luca Ring

iPad on an armchair with Safari and the Hour extension menu open
Hour is a Safari extension for iPhone, iPad and Mac.

Many people don't even know Safari on the iPhone supports extensions. Since iOS 15, Safari web extensions run on iPhone, iPad and Mac, all from a single Xcode project. My app Hour is exactly that kind of extension: it shows on product pages how long you work for a price.

iPad on an armchair with Safari and the Hour extension menu open
Hour is a Safari extension for iPhone, iPad and Mac.

How it's built

A Safari extension has two parts: a regular app that goes on the App Store, and the extension itself. At its core, the extension is a web extension like those you know from other browsers:

  • manifest.json: describes the name, permissions and which scripts run where.
  • Content scripts: JavaScript injected into the web page that can read it or show something on it.
  • Popup: a small HTML window that opens from the button in the Safari toolbar.
  • Background script: logic that runs independently of any page.

In Xcode you start with the “Safari Extension App” template. If you already have an extension for another browser, the xcrun safari-web-extension-converter command turns it into an Xcode project.

A bridge to the native app

The extension can talk to native Swift code. With browser.runtime.sendNativeMessage, the JavaScript sends a message that the SafariWebExtensionHandler answers in Swift. That's how Hour shares settings like your income between app and extension through an App Group, and Swift frameworks such as Foundation Models are available when JavaScript alone isn't enough.

What I learned

  • Ask for few permissions: the fewer websites an extension may read, the more trustworthy it feels. Safari asks users per site or for all sites.
  • Explain how to turn it on: extensions are off after installation. Users enable them in Safari via the menu in the address bar or in Settings under Safari. A good guide inside the app is a must.
  • Robust against other people's HTML: every shop builds its pages differently. That's why Hour specifically detects the price that counts and ignores old prices, shipping and instalments.
  • Stay light: content scripts run on every matching page. They should be fast and never slow the page down.

Is it worth it?

For many ideas, yes. An extension lives exactly where users already are: in the browser. How to make such an app visible on the App Store is covered in App Store optimisation for indie developers.

  • Safari Web Extension
  • Safari extension
  • iOS
  • macOS
  • Xcode