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.
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.
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.
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.
Last updated