4

I have a Safari App Extension. Is there a way to detect from the containing app if the extension was successfully installed and enabled in Safari? The documentation leaves a lot to be desired…

zoul
  • 102,279
  • 44
  • 260
  • 354

1 Answers1

6

Generally the way to check is through SFSafariExtensionState and SFSafariExtensionManager — if it's enabled, then it's installed.

Example:

let extensionIdentifier = "com.acme.MyAppExtension"

@IBOutlet weak var label: NSTextField!
@IBOutlet weak var statusImage: NSImageView!

func checkAppExtension() {
    SFSafariExtensionManager.getStateOfSafariExtension(withIdentifier: extensionIdentifier) { (state, error) in
        DispatchQueue.main.async {
            if (state?.isEnabled ?? false) {
                self.label.stringValue = "MyApp Extension for Safari is enabled"
                self.statusImage.image = NSImage(named: "enabled")
            } else {
                self.label.stringValue = "MyApp Extension for Safari is currently disabled"
                self.statusImage.image = NSImage(named: "disabled")
            }
        }
    }
}
l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • Ha! I must have missed that one, thank you! The API is a mess. – zoul Jan 08 '19 at 11:07
  • You're welcome, there's also [`SFSafariExtensionManager`](https://developer.apple.com/documentation/safariservices/sfsafariextensionmanager?language=objc) which is similar, but can include an identifier and completion handler. – l'L'l Jan 08 '19 at 11:10
  • Well, I have missed that one _too_. So I guess my reading skills are a mess, not the API. Thank you very much! – zoul Jan 08 '19 at 11:14
  • 2
    Is there any way to get notified about state changes of the extension? The guys at AdBlock are doing it somehow, but I don't know if they constantly check for state or they are somehow notified when it's enabled/disabled. – ov1d1u Oct 24 '19 at 16:07
  • 1
    Is there anything similar to iOS? – Vinoth Vino Jun 06 '23 at 09:30