How to run socket.io in the background on iOS in a React Native app?

前端 未结 4 1809
轮回少年
轮回少年 2021-02-07 02:36

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

4条回答
  •  囚心锁ツ
    2021-02-07 03:14

    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. UIBackgroundModesshould be set to audio (check the docs for more information)

提交回复
热议问题