2

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.

Mason G. Zhwiti
  • 6,195
  • 8
  • 55
  • 92
  • Did you ever resolve this? I'm having the same problem. – Nancy Dec 12 '15 at 15:04
  • @Nancy Check the answer I just posted. Not sure exactly why it worked for us, my guess is only that the ajax request was being cancelled because the controller that initiated the request was being unwound at the time of the request (even though the request was actually taking place in another controller...). `¯\_(ツ)_/¯` – Mason G. Zhwiti Dec 14 '15 at 19:53

1 Answers1

1

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
}
Community
  • 1
  • 1
Mason G. Zhwiti
  • 6,195
  • 8
  • 55
  • 92