开发者

Running apps in background?

开发者 https://www.devze.com 2023-03-11 05:27 出处:网络
Is it possible to keep a socket connection alive in background, to be able to push new data and开发者_运维技巧 alert users at all times?The answer to this question is a definitive yes. If you are in t

Is it possible to keep a socket connection alive in background, to be able to push new data and开发者_运维技巧 alert users at all times?


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.


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.


You can definitely alert users with local and push notifications. And as far as I know, you can keep a connection open only for limited time.

Look here for more details.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号