Blocking Ads and Trackers

Block advertising and user tracking URLs to create a safe browsing experience.

Blocking Ads and Trackers on Android and JavaScript

Before checking if a URL is allowed, you should activate at least one ruleset and allow the Ad/Tracker Blocker to compile the rules.

Filtering Status

The Ad/Tracker Blocker client can take some time to compile the rules after changing the active rulesets. Use the status property to determine the state of this process.

import { Status } from '@sudoplatform/sudo-ad-tracker-blocker'

 * Status values:
 * Status.Preparing: 
 *   Calls to checkUrl() will be resolved when status 
 *   becomes `Ready`.
 * Status.Ready:
 *   Filter engine is ready and calls to checkUrl()
 *   will be answered immediately.
 * Status.Error:
 *   An error occured while (re) initializing the 
 *   filter engine.

const status: Status = client.status

Check URL Against Active Rulesets

To check if a URL should be blocked or allowed, call the checkUrl method and supply the URL in question. The input to the checkUrl function consists of two URLs. The first is the URL of a resource that is being requested, and the second is the URL of the main page, if any. If the main page URL is provided and is present in the list of exceptions, content will not be blocked.

If the MIME type of the requested URL is known, it can optionally be provided to aid blocking decisions.

const url = ""
const sourceUrl = ""

const result: CheckUrlResult = await client.checkUrl(url, sourceUrl)

Blocking Ads and Trackers on iOS

The iOS SDK differs from other platforms due to the requirements of Apple's Content Blocker implementation. Whereas the Android and JavaScript SDKs provide a custom blocking engine, the iOS SDK provides ruleset data in the Apple Content Blocking format. The lists provided by the iOS SDK can then be used to block content in WKWebView or the system Safari app.

Fetching Content Blocker Data

Use the getContentBlocker function to retrieve ruleset data in the Apple Content Blocking format. The rulesetData property of the returned ContentBlocker can then be used with a WKWebView or the system Safari app. The ruleset data is a combination of the base ruleset data plus any exceptions added via the client. See Manage Exceptions for documentation on adding/removing exceptions.

Ruleset data is cached for performance, and the most recent ruleset is always fetched if the cache is out of date.

//let client: SudoAdTrackerBlockerClient
//let ruleset: Ruleset

client.getContentBlocker(ruleset: ruleset) { (result) in
    switch result {
    case .success(let contentBlocker):
    case .failure(let error):

/// Represents a base ruleset combined with a list of exceptions.
public struct ContentBlocker {

    /// Generated ID of this content blocker. Based on the base ruleset and an exceptions added.
    public let id: String

    /// The base ruleset used to generate the content blocking json
    public let baseRuleset: Ruleset

    /// The content blocking json in Apples content blocking format.  This can be passed to either a content blocking extension or Webkit for blocking.
    public let rulesetData: String

    /// Exceptions applied to the content blocker.
    public let exceptions: [BlockingException]

The getRuleset function can be used to retrieve the base ruleset data without any exceptions applied.

Using Ruleset Data with a WKWebView

The rulesetData from the iOS SDK can be used to block content in a WebKit View. Refer to Apple's documentation on WKContentRuleList and WKUserContentController for more details.

Compiling a ruleset can be an expensive operation. The id property of a ContentBlocker can be used as a cache key to avoid unnecessary compilation.

let contentBlocker: ContentBlocker // ... see above ...

let store = WKContentRuleListStore.default()
    encodedContentRuleList: contentBlocker.rulesetData
) { (ruleList, error) in
    guard let ruleList = ruleList else {
        // Display or log the error.

    let userContentController = WKUserContentController()

    let configuration = WKWebViewConfiguration()
    configuration.userContentController = userContentController
    let webView = WKWebView(frame: .zero, configuration: configuration)
    // Present the web view.

Last updated