当应用程序确实进入后台时,CLLocationManager 不会开始位置更新
CLLocationManager doesn't start location updates when application did enter background
在我的应用程序中,我需要在后台执行位置更新。为此,我将我的位置跟踪对象注册为观察者,如下所示:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(start)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
这是开始更新位置的方法:
- (void)start
{
NSInteger downloadsCount = [[SGDataManager sharedInstance] countOfActiveDownloads];
NSInteger uploadsCount = [[SGDataManager sharedInstance] countOfActiveUploads];
if (downloadsCount + uploadsCount > 0)
{
[self.locationManager startUpdatingLocation];
}
}
但是位置更新永远不会开始。但是,如果我将 UIApplicationDidEnterBackgroundNotification
更改为 UIApplicationWillResignActiveNotification
,那么位置更新将在后台完美运行。那么我该如何让它在进入后台通知时起作用?
我要注意我的应用启用了位置更新后台模式
您需要启用项目后台模式功能。
转到项目属性 >> 功能部分。
Select 后台模式 然后点击开关打开。
在后台模式下检查位置更新正确。
这将在后台模式下启用位置更新。
当您的应用程序进入后台时,它完成任务的时间有限。在那之后任务将暂停。
尝试将您的 UpdateLocation
放入后台任务,如下所示:
http://hayageek.com/ios-long-running-background-task/
此示例在进入后台时也使用位置更新。
编辑:
我认为这是一个 CoreLocation
错误。它可能是这样的:startUpdatingLocation
方法完成,后台任务完成,但 CoreLocation
产生的其他线程中仍有一些事情发生,应用程序挂起该线程,因为它进入后台。这只是一个猜测。
无论如何,这里有一个解决方法:在后台延长您的应用程序生命周期。 startUpdatingLocation
完成后不要结束后台任务,让它 运行 几秒钟。
__block UIBackgroundTaskIdentifier back = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask: back];
back = UIBackgroundTaskInvalid;
}];
[manager startUpdatingLocation];
此代码将使您的应用 运行 在后台运行几分钟。如果你愿意,你可以创建一个可以更快暂停它的计时器
NSTimer* killBackgroundTaskTimer = [NSTimer scheduledTimerWithTimeInterval: 10 target: self
selector: @selector(killBackgroundTask:) userInfo: nil repeats: NO];
-(void) callAfterSixtySecond:(NSTimer*) t
{
[application endBackgroundTask: back];
back = UIBackgroundTaskInvalid;
}
在我的应用程序中,我需要在后台执行位置更新。为此,我将我的位置跟踪对象注册为观察者,如下所示:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(start)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
这是开始更新位置的方法:
- (void)start
{
NSInteger downloadsCount = [[SGDataManager sharedInstance] countOfActiveDownloads];
NSInteger uploadsCount = [[SGDataManager sharedInstance] countOfActiveUploads];
if (downloadsCount + uploadsCount > 0)
{
[self.locationManager startUpdatingLocation];
}
}
但是位置更新永远不会开始。但是,如果我将 UIApplicationDidEnterBackgroundNotification
更改为 UIApplicationWillResignActiveNotification
,那么位置更新将在后台完美运行。那么我该如何让它在进入后台通知时起作用?
我要注意我的应用启用了位置更新后台模式
您需要启用项目后台模式功能。
转到项目属性 >> 功能部分。
Select 后台模式 然后点击开关打开。
在后台模式下检查位置更新正确。
这将在后台模式下启用位置更新。
当您的应用程序进入后台时,它完成任务的时间有限。在那之后任务将暂停。
尝试将您的 UpdateLocation
放入后台任务,如下所示:
http://hayageek.com/ios-long-running-background-task/
此示例在进入后台时也使用位置更新。
编辑:
我认为这是一个 CoreLocation
错误。它可能是这样的:startUpdatingLocation
方法完成,后台任务完成,但 CoreLocation
产生的其他线程中仍有一些事情发生,应用程序挂起该线程,因为它进入后台。这只是一个猜测。
无论如何,这里有一个解决方法:在后台延长您的应用程序生命周期。 startUpdatingLocation
完成后不要结束后台任务,让它 运行 几秒钟。
__block UIBackgroundTaskIdentifier back = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask: back];
back = UIBackgroundTaskInvalid;
}];
[manager startUpdatingLocation];
此代码将使您的应用 运行 在后台运行几分钟。如果你愿意,你可以创建一个可以更快暂停它的计时器
NSTimer* killBackgroundTaskTimer = [NSTimer scheduledTimerWithTimeInterval: 10 target: self
selector: @selector(killBackgroundTask:) userInfo: nil repeats: NO];
-(void) callAfterSixtySecond:(NSTimer*) t
{
[application endBackgroundTask: back];
back = UIBackgroundTaskInvalid;
}