Search code examples
swiftwebkitwkwebview

How do you create local overrides of HTTP requests in Swift?


In a WKWebView there is a tool called Inspector where it is possible to create "local overrides" which can override the data received. Is there a way to recreate this and read/write to these overrides?

Local Overrides in Safari


Solution

  • I think the tool you are looking for is the WKUserContentController.

    The userContentController is part of the WKWebViewConfiguration that you can provide when creating a WKWebView

    The advantage that the web inspector has is that it is JavaScript running in the context of the page. Using the WKUserContentController you could inject custom JavaScript onto the page. From there a JavaScript developer should be able to duplicate the features of the web inspector.

    The userContentController will also allow you to set content rules for handling restricted content. You might be able to use some of those rules to define content substitutions.