简体   繁体   English

如何在后台运行 iOS4 应用程序?

[英]how can I run iOS4 app in the background?

I'm developing an app for iOS4.我正在为 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.谢谢你。

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).如果您不使用 VoIP、音频或 GPS,您只能使用任务完成模式(在后台限制为 10 分钟)。 To do that you have to tell the OS you want to start a task with:为此,您必须告诉操作系统您要启动任务:

UIApplication*    app = [UIApplication sharedApplication];

UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
}];

and when you're done, you can end it with:当你完成后,你可以结束它:

[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;

Remember that if your running longer than 10 minutes, the OS will kill your app.请记住,如果您的运行时间超过 10 分钟,操作系统将终止您的应用程序。

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.applicationDidEnterBackground:你有你的代码仍然阻塞主线程的问题,这就是当你退出应用程序时它被杀死的原因。

If you want to start executing code in applicationDidEnterBackground: you should begin the background task and dispatch whatever it is you want to do with dispatch_async(queue, block_with_your_code);如果你想在applicationDidEnterBackground:中开始执行代码,你应该开始后台任务并使用dispatch_async(queue, block_with_your_code);你想要做的任何事情。

You can read more on it here你可以在这里阅读更多

There is non you are only allowed to run VOIP, Audio or Locationbased apps in the background.您只能在后台运行 VOIP、音频或基于位置的应用程序。 So unless you apps falls in one of those categories there is no way to keep you app working in the background.因此,除非您的应用程序属于这些类别之一,否则无法让您的应用程序在后台运行。

Apple allows only certain types of apps to run in the background, like navigation and VOIP apps, to name just two. Apple 仅允许某些类型的应用程序在后台运行,例如导航和 VOIP 应用程序,仅举两个例子。 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).唯一的选择是“长时间运行的后台任务”——这允许应用程序在后台继续工作长达十分钟(这个“宽限期”的确切持续时间可能会发生变化,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.您可以在 Hipstamatic 等应用程序上观察到这一点,即使应用程序被移到后台,它也会完成图像的后期制作。

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.您基本上在后台播放用户 iPod 库中的曲目,这使您的应用程序可以在后台工作更长时间。 You can read more about it on Tapbots' site .您可以在Tapbots 的网站上阅读更多相关信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM