ioscocoansuserdefaultssafari-content-blocker

Determine if user has enabled application's Safari content blocker extension


I'm working on a Safari Content Blocking extension. I intend to show setup instructions if the extension is disabled and to show settings if it is conversely enabled. How can I determine if the extension is enabled by the user?

I've seen this method to detect if a custom keyboard is activated but there's no key on NSUserDefaults that relates to Safari Content Blockers.


Solution

  • As of iOS 10, there is a new method in SFContentBlockerManager to support this:

    getStateOfContentBlocker(withIdentifier:completionHandler:)

    And you call it like this (Swift 3):

    SFContentBlockerManager.getStateOfContentBlocker(withIdentifier: "your.identifier.here", completionHandler: { (state, error) in
        if let error = error {
            // TODO: handle the error
        }
        if let state = state {
            let contentBlockerIsEnabled = state.isEnabled
            // TODO: do something with this value
        }
    })