Make a network call made in foreground "survive" app being put to background temporarily in iOS

Please provide the following:

  1. SDK Version: 46
  2. Platforms(Android/iOS/web/all): iOS

The default behaviour in iOS seems to be that network calls made while in foreground are interrupted/cancelled as soon as the app backgrounds.

I have to make a network call to login, part of which requires confirming a code in another app, i.e. I make the login network call, background the app to go another one to confirm some verification number (the login call should still be ongoing in my Expo app) and then I go back to my Expo app and the login call finishes.

This is not working in iOS as the login call gets cancelled as soon as the app is backgrounded.

I’ve tried implementing this as a background task, but I feel weird about that as the use case for background fetch tasks seems to be recurring network calls at some interval, while my use case is just running this network call once, but make sure it survives being backgrounded for a short while.

When I implemented the background task - it also didn’t run immediately, but instead ran after several minutes after being in the background.

My questions are

  • Is there a way to implement my desired behaviour without using expo-task-manager and expo-background-fetch?
  • If I have to use a background fetch task, what can I do to run it immediately (and only once)?

Yeah, background fetch isn’t really designed for this. It’s designed to run a process based on some condition when the app isn’t running (e.g., phone enters a particular location, or it’s been a particular amount of time since data has been refreshed), and it’s guaranteed to run at a specific time.

Ultimately, I think how this login API works would need to be changed. It needs to be able to be called again when the app returns to the foreground to complete the verification, maintaining state via session or some other means. This is similar to how websockets work- you can’t guarantee a socket connection will be maintained on background, and the system has to be resilient to the connection breaking and being reestablished.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.