macossafari-app-extension

Detecting if Safari App Extension is installed and enabled


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…


Solution

  • 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")
                }
            }
        }
    }