Mason G. Zhwiti
Mason G. Zhwiti

Reputation: 6540

iOS 9 WKWebView yields "failed to load resource: cancelled" where iOS 8 doesn't

I'm working on an app that supports iOS 8 and 9, using WKWebView for some views in my app.

For the time-being I've whitelisted my site and subdomains in ATS (awaiting some SSL cert changes). So I don't think what I'm going to describe is related to ATS, but who knows.

My native code calls a javascript function in my WKWebView that triggers an ajax GET request using jQuery.

In iOS 8, it works every time.

In iOS 9, it works only sometimes. Seemingly less than half the time.

When I use Safari Developer Console on the desktop that my iPhone is connected to, to hook up to the WKWebView, I am able to see this error:

Failed to load resource: cancelled

As far as I can tell, the network request is never even made. I've been staring at this problem for 2 days now, and am not able to discern WHY the network request is "cancelled." I do call .abort() on my ajax requests in certain situations, but while I diagnose this issue, I've commented all of those out.

Has anyone else experienced any new ajax issues like this in iOS 9 that weren't happening in iOS 8?

Any idea how I can figure out what is "cancelling" the ajax request? I've tried every debugging trick I can think of, but it's not that easy to debug WKWebView ajax.

Upvotes: 2

Views: 1374

Answers (1)

Mason G. Zhwiti
Mason G. Zhwiti

Reputation: 6540

I can't say for sure why the change below fixed the issue for us, but just try to explain what we did so you can see how it may apply elsewhere.

We're executing some barcode scanning via MTBBarcodeScanner, and in the scanning block, ultimately were ending scanning with code like this:

self.searchController?.decodeBarcode(code) // triggers ajax request, gets cancelled

delay (0.2) {                                               
    self?.performSegueWithIdentifier("unwindFromBarcodeScanner", sender: self)
    self?.scanner?.stopScanning()   
}

This delay() technique is an idea taken from this SO answer, which we implemented as we were encountering some other issues when attempting to stop scanning while we were unwinding.

So after encountering this ajax cancellation issue, we tried moving the ajax call down into the delay block of code, and now we no longer face this problem of it being cancelled. It now looks like this and works fine:

delay (0.2) {                                               
    self?.performSegueWithIdentifier("unwindFromBarcodeScanner", sender: self)
    self?.scanner?.stopScanning()   
    self?.searchController?.decodeBarcode(code) // triggers ajax request, works fine
}

Upvotes: 1

Related Questions