0
我想获取我的设备的位置24x7次。但根据苹果文档,应用程序最多只能运行10分钟。之后,应用程序会自动杀死。 我使用下面的代码:位置服务在后台执行
- (void)applicationWillResignActive:(UIApplication *)application
{
UIApplication * app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
dispatch_async (dispatch_get_main_queue(),^{
if (bgTask != UIBackgroundTaskInvalid)
{
[app endBackgroundTask: bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
}];
// Start the long-running task and return immediately.
dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
lm.locationManager.distanceFilter = kCLDistanceFilterNone;
lm.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[lm.locationManager startMonitoringSignificantLocationChanges];
[lm.locationManager startUpdatingLocation];
dispatch_async (dispatch_get_main_queue(),^{
if (bgTask != UIBackgroundTaskInvalid) {
[app endBackgroundTask: bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
});
}
我也包括支持后台模式为需要的位置。
但该应用程序只运行10分钟。我如何让应用程序始终运行。请帮助我!
你在'didUpdateLocations:'中写了些什么? – samfisher 2013-04-24 07:29:13
现在,我只是在日志中获取位置。自应用程序在后台10-15分钟后,日志停止。 – 2013-04-24 07:33:08
'significantLocationChanges'不要求你实现后台任务。只需删除bgtask创建并使用支持的背景模式键正常运行它 – samfisher 2013-04-24 07:35:50