Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running apps in background?

Is it possible to keep a socket connection alive in background, to be able to push new data and alert users at all times?

like image 354
aryaxt Avatar asked Jun 09 '11 04:06

aryaxt


People also ask

Is it good to have apps running in the background?

Background applications can eat up your battery and utilize the resources. There are some applications which probably won't be optimized well, some might be malicious or have malware, or some applications just have a bug.

How do I see what apps are running in the background?

To open Quick Settings, from the top of the screen, swipe down twice. To see the number of active apps running in the background: At the bottom left, tap # active apps. Or, at the bottom right, tap the number next to Settings and Power .

What does it mean to run apps in the background?

In Windows, apps can continue to perform actions even when you are not actively in the app's window. These are commonly called background apps. You can decide which apps will run in the background, and which won't.


2 Answers

The answer to this question is a definitive yes. If you are in the background state, then you can keep a connection open and process messages from a server.

Unfortunately the complexity here is that you don't have a lot of control over the state your application is in:

  • foreground - The user has tapped your icon and the app is running with the UI visible.
  • suspended - The user was previously running your app in the foreground, but suspended it by hitting the home button or receiving a call. Basically your app is 'freeze dried' and will remain inactive until it is resumed by the user (starting where it left off) or it is terminated by the OS (see below).
  • background - The app was previously running in the foreground but has moved to the background state as a result of something the user has done. Normally your app will move to the suspended state in this case, but there are things you can do as the developer to prevent the instant 'freeze dry' and go into the background instead (see below). Your app will also be in the background state if it is woken up for a significant change event.
  • terminated - Your app has been unloaded from memory and the next time it starts will be from scratch. This is what happens when you double click the home button and then tap the x next to your app icon. It moves the app from the suspended state into the terminated state. This will also happen if the OS decides it needs room for more recently running apps and your app has been suspended for a long time.

So obviously the trick here is how do I stay in the background state as a long as possible. There are a couple of ways to do this:

  • Beg for more time - You can get up to 10 minutes of additional background processing when your app is closed if you ask for it.
  • Use UIBackgroundMode - You can declare youself a voip, audio or location app by adding the corresponding UIBackgroundMode value to the pList. There are special requirements for these types of apps which you can check out here.

So these approaches are not without their own problems (getting approved on the store being one of them) and as such I tend to agree with the other answers that using push notifications is probably your best approach for notifying your users. With the notification improvements in iOS5 this is going to be the best user experience going forward.

like image 68
RedBlueThing Avatar answered Sep 20 '22 06:09

RedBlueThing


You can keep a socket connection alive (or do whatever else you want) in the background for about 15 minutes after your app closes. There are also more specialized background processing modes (specifically, audio, voip, and location) which Apple supports if your app fits into one of their supported categories. See here.

If you want to keep sending the user notifications indefinitely, you want to use the Apple Push Notification Service. This allows your app to continue to receive notifications when it's not running, and it conserves resources since there's only one connection to the APN service at a time.

like image 25
JonathonW Avatar answered Sep 24 '22 06:09

JonathonW