iOS在600秒后终止了我的后台任务

4

我正在开发一个应用程序,需要每30分钟更新位置的后台任务。我尝试使用NStimer并每分钟更新一次,它可以工作(花费了2个小时)。然而,当我将其设置为30分钟时,iOS在10分钟后将我的应用程序关闭。

日志显示:

Sun Oct 16 11:29:47 unknown SpringBoard[32] <Warning>: MyApp[315] has active assertions beyond permitted time: 
    {(
        <SBProcessAssertion: 0xbb34040> identifier: UIKitBackgroundCompletionTask process: MyApp[315] permittedBackgroundDuration: 600.000000 reason: finishTask owner pid:315 preventSuspend  preventIdleSleep 
    )}
Sun Oct 16 11:29:47 unknown SpringBoard[32] <Warning>: Forcing crash report of WhereAreYouReg[315]...
Sun Oct 16 11:29:48 unknown SpringBoard[32] <Warning>: Finished crash reporting.

我的后台任务是通过以下代码启动的:

backgroundTaskIdentifier = [[UIApplication sharedApplication]
                   beginBackgroundTaskWithExpirationHandler:^{
                       // If you're worried about exceeding 10 minutes, handle it here
                   }];
    theTimer=[NSTimer scheduledTimerWithTimeInterval:30*60.0
                                              target:self
                                            selector:@selector(updateLocation)
                                            userInfo:nil
                                             repeats:YES];

updateLocation开始更新位置,当接收到位置后,它会停止更新位置以节省电池。

如何执行此后台任务?有什么帮助可以防止我的应用程序在600秒后被杀死吗?

非常感谢!

1个回答

2

您无法实现这个。没有办法创建持续超过10分钟的后台任务。


即使在 .plist 中设置了“所需的后台模式”为位置,也可以吗? - Ibai
好的。理论上你可以注册一个“我的位置已经显著改变”的通知并执行一些代码,可以参考这个链接:http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html - ckruse

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接