GitXplorerGitXplorer
S

AdsBlock_WKWebView_for_iOS11

public
41 stars
19 forks
1 issues

Commits

List of commits on branch master.
Unverified
09d47063b1cd63a9066c32b7897e35ccd5d32a95

Improve compile time

ffok8 committed 7 years ago
Unverified
512c46969f7bcdf88c94368a5dab7ead0eafa613

Fix error handlings

ffok8 committed 7 years ago
Unverified
00430708da154ed7bcae566c5c714c019f22e434

Improve readability

ffok8 committed 7 years ago
Unverified
4da5ab32d32971af0bf20e9951a4e826c91fedd9

Fix user defaults synchronizing

ffok8 committed 7 years ago
Unverified
b60a5042485f7092796fa815e92fc4d87db65b5f

Fix regex of rule list

ffok8 committed 7 years ago
Unverified
1fa3c5558ec6a42a430f79b94e60db97e5b7f7b3

Fix typo on a link

ffok8 committed 7 years ago

README

The README file for this repository.

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.

Steps

  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 'https://adaway.org/hosts.txt' | 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

PR