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

眉间皱痕 提交于 2020-01-09 19:44:28

问题


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.


回答1:


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



回答2:


You could utilize a SFSafariViewController to load a custom website. This website checks whether it is able to show something that your content blocker should block. Then redirect to the respective custom url (success/failure) that your app previously registered for. You could even use a hidden Safari View Controller without animation to avoid any distraction from the user's perspective (as shown here). (I guess this technique is used by former content blocker Peace)

Steps

App

  1. Register custom URLs for success/failure
  2. Register for notification callback using the NotificationCenter (e.g. contentBlockerEnabled)
  3. Use SFSafariViewController to show a custom website and include the following rule in blockerList.json:

    {
        "action": {
            "type": "css-display-none",
            "selector": ".blocked_selector"
        },
        "trigger": {
            "url-filter": ".*"
        }
    }
    

Website

  1. Check for blocked content:

    if($('.blocked_selector').css('display') == "none") {
       // Content blocker enabled
    }
    
  2. Redirect to custom URL (success/failure)

App

  1. Post notification from application:openURL:options: (success/failure based on called url)

Update 18/01

Following on from Tilo's hypothesis, I built the proposed solution. I wrote about what I learnt on Medium and you can grab the source files from GitHub.

TL;DR It works but only temperamentally due to the latency incurred of the content blocking rules database to update. A potential workaround is redirecting the test page to create an artificial delay.



来源:https://stackoverflow.com/questions/33452917/determine-if-user-has-enabled-applications-safari-content-blocker-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!