I am using socket.io
in an iOS React Native(v0.20) app
. The app tracks my location, and when my position changes it emits a message to a server. If the
In your picture of the Info.plist, it only allows the following three background modes:
Background fetch
Location updates
Remote notifications
You should allow "Audio and AirPlay" mode and it seems like "App plays audio or streams audio/video using Airplay".
iOS is fairly restrictive on the kind of processes it allows when an app is backgrounded.
There seems to be some discussion around this on the socket.io client issue tracker.
It seems someone got the socket connection to remain active if they set the background mode to audio
. YMMV
Make sure you're setting the correct values in the UIBackgroundModes section of the Info.plist file. How to do this in XCode
I know there are hacks to get this done (i.e. the audio
hack), but there's a reason iOS doesn't allow long running tasks in the background - please rethink your design to allow a better product.
Using log running tasks will deplete the battery. If you need data updates, register to get push notifications using the Apple push service (it's free, if your doing it directly, or it was last time I dealt with a similar issue).
Location tracking, specifically, is an allowed background system service, consider using the native API for this.
As to pushing the data to the server, you won't be able to (and probably shouldn't) maintain a long lasting connection with the server.
Consider collecting data and uploading to the server the collected batch every once in a while - preferably when the app is reopened, so you don't waste resources in the background and get people upset about their battery life and their data consumption.
There is no way you can do this "the right way".
Edit
From Apple's official documentation :
Implementing Long-Running Tasks
For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:
Apps that play audible content to the user while in the background, such as a music player app
Apps that record audio content while in the background
Apps that keep users informed of their location at all times, such as a navigation app
Apps that support Voice over Internet Protocol (VoIP)
Apps that need to download and process new content regularly
Apps that receive regular updates from external accessories
Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.
This being said, there is a working technique exist to keep your socket open :
Play a silent sound and set the background mode to audio. Check this article for more information on the matter (it's a bit old but still valid). This will allow you to keep your socket open faking an audio app. Your info.plist should be updated to allow audio to run in the background.
UIBackgroundModes
should be set to audio
(check the docs for more information)