Skip to content

Instantly share code, notes, and snippets.

@mackuba
Last active April 19, 2023 13:51
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mackuba/e81558926e9a09f06e58 to your computer and use it in GitHub Desktop.
Save mackuba/e81558926e9a09f06e58 to your computer and use it in GitHub Desktop.
Tips for writing iOS content blockers - HelsinkiOS
  • read this first: https://www.webkit.org/blog/3476/content-blockers-first-look/
  • start by adding a new extension target to your iOS app of type “content blocker”
  • launch the app using the main target’s scheme + a call to SFContentBlockerManager.reloadContentBlockerWithIdentifier() with the extension’s id in application:didFinishLaunchingWithOptions: to auto-reload the blocker in development mode
  • if you don’t call reloadContentBlockerWithIdentifier() then you need to switch the blocker off and on again in the Safari settings (stop the app in Xcode if the switch is not moving)
  • use inspector from desktop Safari to inspect the Safari in the simulator in order to find specific things to block
  • things like periods in the url-filter regexp need to be escaped with double backslashes, e.g. facebook\\.net
  • if you use if-domain, it needs to be an array, even for one element
  • domain foo.com might not match www.foo.com even though I think it’s supposed to (UPDATE: They've changed it in one of the betas, now foo.com only matches foo.com and to match all subdomains you need to use *foo.com - thanks @CraftyDeano!)
  • remember that extensions are separate from the main app - use app groups and shared app group containers to share data between the app and the extension
  • json file can be updated silently or even generated on the fly, just return whatever you want to in the beginRequestWithExtensionContext: callback
  • look for critical errors in ~/Library/Logs/CoreSimulator/*/system.log
@NickG-DK
Copy link

NickG-DK commented Mar 31, 2021

I have read your tips on the content blocker extension, which are great. I have been told that I have to call SFContentBlockerManager.reloadContentBlockerWithIdentifier() in my main app to reload the content blocker. But my problem is i dont know HOW to call it correctly. I tried using a simple button but that didnt work. Can you help me out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment