zoul
zoul

Reputation: 104065

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…

Upvotes: 5

Views: 1560

Answers (1)

l'L'l
l'L'l

Reputation: 47169

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

Upvotes: 7

Related Questions