2

I have a hybrid app that runs on iOS, and uses wkWebView (the language is objective c).
I am trying to intercept a GET request from the javascript side, in the iOS wkWebView side.
In javascript, I make a fetch, or xhr request call, but it is not intercepted in wkWebView.
(An equivalent version of the app that runs on Android, and uses webView works ok - the fetch request, is intercepted in webView via shouldInterceptRequest.)

If I do a window.location.href call, the call is intercepted (in decidePolicyForNavigationAction) (but this is not a request).
But if I do fetch or xhr (XMLHttpRequest) request, the call is NOT intercepted.

Several questions on the Internet like this ask about how to intercept POST requests.
My case is even more basic. I cannot intercept a GET request

My code looks like this:

Javascript code:

let url = 'http://example.com';

// try3 - using href - call is intercepted in iOS (but this is not a request...)
window.location.href = url;

# --------------------------------------------------------------

// try2 - using xhr - call is NOT intercepted in iOS
function makeRequest (method, url, done) {
    var xhr = new XMLHttpRequest();
    xhr.open(method, url);
    xhr.onload = function () {
        done(null, xhr.response);
    };
    xhr.onerror = function () {
        done(xhr.response);
    };
    xhr.send();
}

makeRequest('GET', url, function (err, datums) {
    if (err) { throw err; }
    console.log(datums);
});

# --------------------------------------------------------------

// try1 - using fetch - call is NOT intercepted in iOS
response = await fetch(url);

native Objective-C code:

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  
    DTLogDebug (@"BEG decidePolicyForNavigationAction");
    ... 

How can iOS intercept xhr ajax requests?

Thanks

Avner Moshkovitz
  • 1,138
  • 1
  • 18
  • 35

0 Answers0