WKWebView block certain scripts while loading any URL (Block tracking or Ads in any website)

919 views Asked by At

I'm working on a iOS (swift) Secure Browser Project. I need to implement one functionality in which we can show users how many tracking scripts are running in current WKWebView and ability to stop those tracking script. (Just like Ghostery Privacy Browser)

I'm able to track and list the scripts embedded in web page (I do this after scanning the content which has already loaded in the delegate didfinishload is called). But I'm not sure how do I stop them.

For fetching all scripts in webpage I'm reading all script tags in innerHTML.

Please help me stop executing certain scripts in WKWebView.

Thank you in advance

1

There are 1 answers

0
Mindaugas On

I don't think that reading all script tags is the way to detect scripts loaded by web page as it is very error prone.

For your task you would be better served by using an old UIWebView and implementing custom NSURLProtocol to do blocking/filtering. It is not possible to intercept all the request made by WKWebView (as it is loaded out of process and you get as much callbacks as Apple has implemented in WKNavigationDelegate). Contrary you can control everything loaded by UIWebView with simple custom NSURLProtocol implementation.