XHR requests are canceled in cordova-ios after the screen is locked

114 Views Asked by At

in our cordova ios app there is a case, when user can save form using xhr request.

In normal scenario when the Iphone screen is not locked after starting the save request - everything works fine.

But when the screen gets locked right after starting save request - request fails with error - Failed to load resource: The network connection was lost.

I have made some research on this issue and found Failed to load resource: The network connection was lost Safari It is said that WKWebView has been "optimized" to not make network connections while screen is locked As far as I understand cordova-ios is using WKWebView so it may inherit this limitation.

  1. Can you, please, clarify if this is expected behaviour for xhr requests to be canceled when the screen is locked on cordova for ios devices?

  2. Are there any workarounds to deal with this issue - I mean, user starts save request, locks the screen, and request is completed successfully? I have also tried using advanced http plugin https://github.com/silkimen/cordova-plugin-advanced-http#readme - the request is not canceled immediately, but when the time spent in background exceeds timeout - request is canceled by timeout.

The issue is observed on IOS platform only.

cordova ios version 6.3.0

Thank you for any help.

2

There are 2 best solutions below

1
Eric On

1- Yes

2- You could try using a background task to save the data. check out https://github.com/katzer/cordova-plugin-background-mode

1
BigBalli On

You get 10seconds to complete a task before it is suspended. This makes me think your XHR is taking a very long time. I would advise making it faster.