2015-12-22 90 views
1

我试图让我的iOS应用程序在后台运行下去,(或位置采样和诊断有关的位置),我发现这个代码 -NSRunLoop后台任务有效

[self.locationManager stopUpdatingLocation]; 
self.timer = [NSTimer scheduledTimerWithTimeInterval:self.currentTimerTime target:self selector:@selector(checkLocation) userInfo:nil repeats:NO]; 
[[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes]; 
[[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:nil]; 

这是伟大的工作但我有一种感觉,苹果不会这样,是最佳做法?

回答

0

正如你可能已经发现,虽然这在开发中工作,它不会在生产中工作。应用程序获得beginBackgroundTaskWithExpirationHandler的执行时间有限,无限制开发,但通常在已发布应用程序中为60秒,届时将调用到期处理程序(您未在示例代码中分配),并且预计您的应用程序将结束后台任务使用由beginBackgroundTaskWithExpirationHandler调用返回的ID(您没有捕获)。不这样做会导致你的应用程序被终止。欲了解更多信息,请参阅相关的问题/答案。

objective c - Proper use of beginBackgroundTaskWithExpirationHandler