How to enable Safari App Extension programmatically?

1.3k views Asked by At

I'm developing a Safari App Extension inside a macOS app. When a user installs this app, the extension is added to Safari, but it's disabled by default. We can detect the state of extension by using SFSafariExtensionManager class via its getStateOfSafariExtension method.

Now I want to enable the extension state programmatically if it is disabled. How can I achieve that? Or do anyone have any idea where the preferences / app extensions settings are stored in macOS?

1

There are 1 answers

3
seclace On

You can create a button such as "Open extension preferences" to show Safari preferences directly for your extension then the user could enable it.

The code for your app:

import SafariServices

func enableExtension () {
    SFSafariApplication.showPreferencesForExtension(withIdentifier: YOUR_EXTENSION_IDENTIFIER) { (error) in
        NSLog("Error \(String(describing: error))")
    }
}

SFSafariApplication could be used in Cocoa app only (not extension).