Skip to content
This is a sample app. iOS 11 introduces Safari like Content-Blocking Rules.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Ads Blocking WKWebView for iOS 11

This is a sample app so you can use this project's code as you want.

iOS 11 introduces Content Filter on WWDC 2017 Customized Loading in WKWebView

A common case we hear from developers is they're working on a browser targeted for schools, libraries, some other public place where the content loaded in the browser has to be family friendly. So we need to filter out everything is not family friendly. In a similar vane, we've heard from developers working on corporate intranet applications that they have varying needs within the same app.

One WebView might need to block all content that is not coming from the corporate's network.

Another WebView might need to block all content coming from certain specific servers.

You can have each WebView setup to do its own thing on a per sub resource basis.


  1. Prepare json format rule lists (literal strings or files).
  2. Compile them in runtime code with each identifier.
  3. Register each of them with the identifer.

When you provide your rule list to WebKit, WebKit compiles into an efficient byte code format. This is kind of an implementation detail that's not directly relevant to you. I'm bringing it up because I want to assure you that a content rule list even a large set of thousands of rules we've been spending a lot of time working on making that as efficient as possible. And no matter how big your rule set is, if it compiles successfully you should not see degradation in loading performance. You supply your rules in a simple JSON format.

When we compile a rule list from JSON to the efficient byte code format, you can name it.

And then later you can look up by the same identifier so you don't have to compile it again.

WebKit stores it on the storage of the device and can look it up much quicker later.

WKWebView's Content-Blocking Rules is the same as Safari's. Creating Safari Content-Blocking Rules

Ads Block Hosts

This sample app borrows host list from Adaway.

And conviert host file to json format by using this command line:

curl -ls '' | grep -E '^[0-9]' | grep -v -E '\s*localhost\s*$' | perl -pe 's/^[0-9.]+\s(\S+)\s*.*$/"$1",/mg' | perl -pe 's/\n//g' | perl -pe 's/\A(.+),\z/[{"trigger":{"url-filter":".*","if-domain":[$1]},"action":{"type":"block"}}]/g' | tr "[:upper:]" "[:lower:]" > adaway.json


You can’t perform that action at this time.