DEV Community

Enes
Enes

Posted on

Building a High-Performance Safari Content Blocker on iOS with Declarative WebKit Rules

Introduction: The State of Ad Blocking on iOS

When developing ad-blocking solutions on iOS, developers face a major architectural choice between:

  1. Declarative WebKit Content Blockers (SFContentBlockerManager)
  2. Local VPN Packet Tunnels (NEPacketTunnelProvider)

While local VPN proxies can inspect all device traffic, they introduce severe downsides: significant battery consumption, latency spikes, and severe privacy concerns because user network packets pass through an active background tunnel.

To provide a private, seamless experience, we engineered AdBlocker Pro: Block Ads using Apple's declarative WebKit Content Blocker API.


Declarative Architecture vs VPN Tunnels

1. Zero-Knowledge On-Device Execution

With SFContentBlockerManager, the iOS host application compiles a declarative JSON ruleset and hands it off directly to the WebKit subsystem:

  • The app never sees URLs, tabs, or DNS queries.
  • Rule matching runs directly inside Apple's native C++ WebKit pipeline.
  • If the host app crashes or is terminated, blocking remains 100% active.

2. Overcoming Rule Limits with Modular Content Extensions

WebKit enforces strict limits per content blocker extension. AdBlocker Pro solves this through modular classification:

  • Advertising Trackers: Google DoubleClick, Criteo, Taboola, Outbrain.
  • Privacy Telemetry: Fingerprinting scripts, cross-site beacons.
  • Cosmetic Filters & Cookie Banners: CSS selectors hiding modal dialogs and GDPR consent walls.

Rules are dynamically packaged into App Group shared storage and compiled into WebKit via:

SFContentBlockerManager.reloadContentBlocker(withIdentifier: extensionIdentifier) { error in
    if let error = error {
        print("Rule compilation error: \(error.localizedDescription)")
    } else {
        print("WebKit rules compiled successfully.")
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Native SwiftUI Performance

  • Dynamic rule toggles with instant App Group synchronization.
  • Real-time whitelist management with zero latency.
  • Ultra-low memory footprint (~12 MB RAM).

App Store Availability & Resources

Have you built Safari WebKit extensions or content blockers on iOS? Let's discuss performance tips and rule compilation strategies in the comments!

Top comments (0)