在后台运行应用程序? [英] Running apps in background?

查看:157
本文介绍了在后台运行应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在后台保持套接字连接活动,以便能够随时推送新数据并提醒用户?

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:


  • 前景 - 用户已经点击了您的图标,应用程序正在运行且UI可见。

  • 已暂停 - 用户之前在前台运行您的应用,但是通过点击暂停主页按钮或接听电话。基本上你的应用程序是冻干的,并且在用户恢复之前(从它停止的地方开始)或者由操作系统终止(见下文),它将保持不活动状态。

  • background - 该应用程序以前在前台运行,但由于用户已完成某项操作而已移至后台状态。通常情况下,在这种情况下,您的应用会转移到暂停状态,但有些事情您可以作为开发人员来防止即时冻干并进入后台(见下文)。如果为后台状态api-when-terminated-suspended>重大更改事件

  • 已终止 - 您的应用已从内存中卸载并在下次启动时将从零开始。这是双击主页按钮然后点击应用程序图标旁边的x时发生的情况。它将应用程序从挂起的状态移动到已终止状态。如果操作系统决定它需要为最近运行的应用程序提供空间并且您的应用程序已被暂停很长时间,也会发生这种情况。

  • 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:


  • 请求更多时间 - 您最多可以获得10个如果您要求它

  • 使用UIBackgroundMode - 你可以声明自己通过将相应的UIBackgroundMode值添加到pList, voip 音频位置应用。这些类型的应用程序有特殊要求,您可以查看这里

  • 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.

所以这些方法并非没有自己的问题(在商店获得批准)其中一个)因此我倾向于同意使用推送通知可能是您的通知用户的最佳方法。借助 iOS5中的通知改进,这将成为最佳用户未来的经验。

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.

这篇关于在后台运行应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆