如何在后台运行iOS4应用程序?
我正在为 iOS4 开发一个应用程序。该应用程序由两个主要组件组成,一个在后台运行,另一个不断显示在屏幕上并从第一个组件获取数据。问题是:第一个组件工作得很好,直到它被放入后台。此时它停止发送数据。这是为什么?有什么解决方法吗?
谢谢。
I'm developing an app for iOS4. The application is made of two main components, one that is supposed to run in the background and one that is constantly displayed on screen and takes data from the first one. Here's the problem: the first component works just fine until it is put in the background. At that point it stops sending data. Why is that? Is there any workaround?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您不使用 VoIP、音频或 GPS,则只能使用任务完成模式(后台时间限制为 10 分钟)。
要做到这一点,你必须告诉操作系统你想要开始一个任务:
当你完成后,你可以结束它:
请记住,如果你的运行时间超过 10 分钟,操作系统将杀死你的应用程序。
在
applicationDidEnterBackground:
中,您的代码仍然会阻塞主线程,这就是为什么它在您退出应用程序时被终止的原因。如果您想开始在
applicationDidEnterBackground:
中执行代码,您应该开始后台任务并分派您想要执行的任何操作dispatch_async(queue, block_with_your_code);
您可以阅读更多信息 此处
If you're not using VoIP, Audio or GPS you can only use the task completion mode (which is limited to 10 minutes in background).
To do that you have to tell the OS you want to start a task with:
and when you're done, you can end it with:
Remember that if your running longer than 10 minutes, the OS will kill your app.
In
applicationDidEnterBackground:
you have the problem that your code still blocks the main thread, which is why it's killed when you exit the app.If you want to start executing code in
applicationDidEnterBackground:
you should begin the background task and dispatch whatever it is you want to do withdispatch_async(queue, block_with_your_code);
You can read more on it here
您只能在后台运行 VOIP、音频或基于位置的应用程序。
因此,除非您的应用程序属于这些类别之一,否则无法让您的应用程序在后台运行。
There is non you are only allowed to run VOIP, Audio or Locationbased apps in the background.
So unless you apps falls in one of those categories there is no way to keep you app working in the background.
Apple 只允许某些类型的应用程序在后台运行,例如导航和 VOIP 应用程序,仅举两例。但即便如此,这些也仅限于必要的任务。
唯一的选择是“长时间运行的后台任务” - 这允许应用程序在后台继续工作长达十分钟(据我所知,此“宽限期”的确切持续时间可能会发生变化)。您可能会在 Hipstamatic 等应用程序上观察到这一点,即使应用程序移至后台,该应用程序也会完成图像的后期制作。
Apple allows only certain types of apps to run in the background, like navigation and VOIP apps, to name just two. But even those are limited to only the necessary tasks.
The only alternative are "longrunning background tasks" - this allows an app to continue working in the background for up to ten minutes (the exact duration of this "grace period" is subject to change, afaik). You may obvserve this on apps like Hipstamatic, which will finish postproduction on images even when the app is being moved to the background.
正如其他人指出的那样,没有真正的方法可以做到这一点,但有些应用程序使用了一种解决方法。您基本上可以在后台播放用户 iPod 库中的曲目,这使您的应用程序能够在后台运行更长时间。您可以在 Tapbots 网站 上了解更多相关信息。
As others have pointed out there's no real way to do this, but there is a workaround some apps use. You basically play a track from the users iPod library in the background, which enables your app to stay working in the background for a longer time. You can read more about it on Tapbots' site.