In the iOS WKWebView, is there a way to detect when the website makes a resource request (e.g. an HttpRequest via Ajax)?
In Android, there is a very convenient method "shouldInterceptRequest", but I cannot find an equivalent in iOS.
In the iOS WKWebView, is there a way to detect when the website makes a resource request (e.g. an HttpRequest via Ajax)?
In Android, there is a very convenient method "shouldInterceptRequest", but I cannot find an equivalent in iOS.
2
Answers
You can inspect the requests from a
WKWebView
using theWKNavigationDelegate
.You may override
webView:decidePolicyFor:...
to inspect the request, check for its type (e.g. link clicked, form submitted, etc.) and inspect the associatedURL
to take some action:As we know we can NOT
WKWebViewConfiguration.setURLSchemeHandler
with http/https schemes because of the error:This happens because
WKWebView.handlesURLScheme
is called inside and generates this error in case of returnedtrue
. Thus we can use an approach by swizzling this method and it allows to handle http/https schemes as well as custom ones.Now we can set http/https schemes and intercept all
fetch
andXMLHttpRequest
insideWKWebView
, for instance:Outputs: