开发者

how can I run iOS4 app in the background?

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 stop开发者_如何转开发s 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). 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.

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


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜