Preventing websockets from connecting when going to background ios swift

After a lot of research, it seems like a gray area ...

I'm trying to send random network requests over websockets after a user logs into the background (using the Swift library Starscream, but I believe the problem is with iOS and sockets in general). Soon after the user exits the application, the socket connection is automatically disconnected. I believe this is due to Apple's policy regarding third-party network activity in the background.

In my AppDelegate application, I tried to reconnect when the user leaves, etc., but it doesn't work. I've also heard of workarounds related to audio playback, but it seems to stop my app from publishing to the App Store (if it doesn't, then why and how does it work?). Others say that I need some kind of grant from Apple, how can I request this? It would be great if someone cleaned up this and provided a legitimate solution. I feel like this is what applications should be capable of, so I'm waiting to find a solution.

+3


source to share


2 answers


I believe there is no legal way to really get around this. Apple does not want apps to do anything in the background because background activity is a big drain on the battery and iPhone users may feel that their battery is running low (in addition to other issues like "unexplained" network usage etc.), so they provide very limited background activity in iOS apps for the convenience of users. However, we can keep the application alive:

From the iOS App Programming Guide :

Whenever you need to keep an app running in the background, iOS can help you do it efficiently and without using system resources or user battery. The methods offered by iOS fall into three categories:

  • Apps that run a short foreground task may ask for a time to complete that task when the app is moved to the background.
  • Applications that initiate downloads in the foreground can override those downloads to the system, thereby allowing the application to be paused or terminated during download.
  • Applications that need to run in the background to support certain types of tasks can declare support for one or more background execution modes.

So it seems that, other than asking iOS to allow the app to finish short tasks or downloads, the only way to ask the system to let the app run in the background is to specify the background execution mode in our Info.plist

. This can be done in the Xcode dialog box Capabilities

for your project, or by editing the property list file directly. Check which background execution modes are available with us:

On iOS, only certain types of apps are allowed to run in the background:

  • Applications that play audio content for the user in the background, such as a music player application.
  • Apps that record audio content in the background
  • Apps that continuously inform users of their location, such as a navigation app
  • Voice over Internet Protocol (VoIP) applications
  • Applications that need to be downloaded and processed regularly.
  • Apps regularly receiving updates from external accessories

Saving a live nest might end up in "Services that need to regularly download and process new content", so let's check that:



Ability to receive small amounts of content opportunistically

Applications that periodically check for new content can ask the system to wake them up so that they can initiate a fetch operation for that content. To support this mode, enable the Background Fetch option from the Background Modes section of the Features tab in your Xcode project. (You can also enable this support by including the UIBackgroundModes key with the fetch value in your application's Info.plist file.) Enabling this mode does not guarantee that the system will present your application at any time to perform background settings. The system must balance your applications to receive content with the needs of other applications and the system itself. After evaluating this information, the system gives time to applications when there are good opportunities to do so.

So it seems that this option is only used to fetch a small amount of content via HTTP requests (or other network requests), not the two-way persistent communication that a websocket will allow you to use. In fact, looking at the other related answers , it seems that there really is no legal way to keep the socket open when the application goes into the background.

This means that you cannot use Web sites as your only communication channel to do what you want. I recommend that you either use the background fetch

(as described above) to receive content in large chunks than use the web app when the app is in the background, or if you want the user to be able to see new content is available, you you can implement push notifications .

You cannot use Push Notifications to send a lot of content directly, but they can be used to prompt the user to have new content when they open your app. Whether you are using background fetch or push notifications, you must implement methods in your app div that synchronize app state with your backend state when your app is returned from background state.

Finally, with regards to using sound as a workaround: a key state element audio

will allow your app to remain in the background indefinitely, but if your app doesn't really use it to play sound, it will be rejected in the app store.

+3


source


I'm not sure what your app is doing, so I can promise Apple will approve, but you'll need Backgroundmodes permission to do this.

In Xcode:

enter image description here



Here is a link to Apple Docs about adding rights to your app.

Here are the docs for programming background execution in iOS.

Once you've enabled the Xcode entitlement and added the required types to your plist, you can start coding your background website connection.

+1


source







All Articles