我正在构建一个应用程序,它使用CoreLocation有两个目的:
在大多数情况下,当应用程序处于后台时,信号设备的断开回调就会发生。因此,应用程序必须在后台调用-startUpdatingLocation
来启动GPS跟踪。
这就是问题所在。当应用程序处于后台时,iOS似乎不启用位置后台更新,如果-startUpdatingLocation
调用发生在后台。
显然,我已经正确配置了背景模式NSLocationAlwaysUsageDescription
,将属性_locationManager.allowsBackgroundLocationUpdates
设置为YES
。
令人惊奇的是:如果我在前台时调用-startUpdatingLocation
,然后切换到后台,则在后台时正确地发送位置更新。但是,在后台调用-startUpdatingLocation
时,应用程序会收到2或3个位置更新,然后永远暂停。直到我手动将应用程序带回前台,当位置更新恢复,然后继续工作,即使我再次切换到后台应用程序。
有人知道或明白我该怎么做才能解决这个问题吗?还是iOS的一个主要制约因素?由于我的应用程序的要求,我真的需要开始定位更新时,应用程序在后台(在回调的信标,确实退出区域)。
顺便说一句,如果你想复制这个问题是很容易的。只需在Xcode中创建一个空白项目,并将此代码添加到默认视图控制器中即可。记住要启用背景模式,在info.plist键"NSLocationAlwaysUsageDescription“中添加一个描述。
- (void)viewDidLoad
{
[super viewDidLoad];
_locationManager = [[CLLocationManager alloc] init];
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_8_3)
_locationManager.allowsBackgroundLocationUpdates = YES;
_locationManager.delegate = self;
_locationManager.activityType = CLActivityTypeOther;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = kCLDistanceFilterNone;
[_locationManager requestAlwaysAuthorization];
// Creating a background task to delay the start of the startUpdatingLocation
__block UIBackgroundTaskIdentifier taskIdentifier = UIBackgroundTaskInvalid;
taskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:taskIdentifier];
}];
// Delaying the call to startUpdatingLocation 10 seconds, enough time to dismiss the application
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[_locationManager startUpdatingLocation];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] endBackgroundTask:taskIdentifier];
});
});
}
然后,通过执行一些CLLocationManager来跟踪NSLogs委托,看看会发生什么。
谢谢,
发布于 2016-08-17 09:48:30
自从你贴出这个问题以来已经三个月了。你自己已经想出来了吗?
我试用了你的密码。我将一些NSLogs
放在AppDelegate
委托函数和viewDidLoad
中。另外,在AppDelegate
类中,我注销了留下的后台时间。
如果在进入后台之前调用了startUpdatingLocation
:
2016-08-18 01:22:52.355 test background GPS from StackOF[2267:745042] viewDidLoad
2016-08-18 01:22:52.376 test background GPS from StackOF[2267:745042] _locationManager.allowsBackgroundLocationUpdates = YES;
2016-08-18 01:22:52.382 test background GPS from StackOF[2267:745042] _locationManager requestAlwaysAuthorization
2016-08-18 01:22:52.410 test background GPS from StackOF[2267:745042] applicationDidBecomeActive
2016-08-18 01:22:52.469 test background GPS from StackOF[2267:745042] viewDidAppear
2016-08-18 01:23:03.361 test background GPS from StackOF[2267:745042] [_locationManager startUpdatingLocation];
2016-08-18 01:23:03.362 test background GPS from StackOF[2267:745042] start updating location, _locationManager.allowsBackgroundLocationUpdates == YES
2016-08-18 01:23:03.382 test background GPS from StackOF[2267:745042] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 65.000000
2016-08-18 01:23:03.630 test background GPS from StackOF[2267:745042] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 1743.000000
2016-08-18 01:23:03.709 test background GPS from StackOF[2267:745042] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 1743.000000
2016-08-18 01:23:03.716 test background GPS from StackOF[2267:745042] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 1743.000000
2016-08-18 01:23:03.720 test background GPS from StackOF[2267:745042] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 1743.000000
2016-08-18 01:23:03.814 test background GPS from StackOF[2267:745042] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 65.000000
2016-08-18 01:23:05.004 test background GPS from StackOF[2267:745042] inner block [[UIApplication sharedApplication] endBackgroundTask:taskIdentifier];
2016-08-18 01:23:05.005 test background GPS from StackOF[2267:745042] inner block start updating location, _locationManager.allowsBackgroundLocationUpdates == YES
2016-08-18 01:23:08.287 test background GPS from StackOF[2267:745042] applicationDidEnterBackground. Background time left: 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000
2016-08-18 01:23:10.319 test background GPS from StackOF[2267:745042] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 65.000000
2016-08-18 01:23:19.321 test background GPS from StackOF[2267:745042] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 65.000000
/****
Keep on polling...
****/
如果在进入后台后调用startUpdatingLocation
:
2016-08-18 01:34:29.023 test background GPS from StackOF[2285:747132] viewDidLoad
2016-08-18 01:34:29.030 test background GPS from StackOF[2285:747132] _locationManager.allowsBackgroundLocationUpdates = YES;
2016-08-18 01:34:29.033 test background GPS from StackOF[2285:747132] _locationManager requestAlwaysAuthorization
2016-08-18 01:34:29.047 test background GPS from StackOF[2285:747132] applicationDidBecomeActive
2016-08-18 01:34:29.104 test background GPS from StackOF[2285:747132] viewDidAppear
2016-08-18 01:34:31.612 test background GPS from StackOF[2285:747132] applicationDidEnterBackground. Background time left: 179.964144
2016-08-18 01:34:40.004 test background GPS from StackOF[2285:747132] [_locationManager startUpdatingLocation];
2016-08-18 01:34:40.006 test background GPS from StackOF[2285:747132] start updating location, _locationManager.allowsBackgroundLocationUpdates == YES
2016-08-18 01:34:40.290 test background GPS from StackOF[2285:747132] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 10.000000
2016-08-18 01:34:40.332 test background GPS from StackOF[2285:747132] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 355.117789
2016-08-18 01:34:40.336 test background GPS from StackOF[2285:747132] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 355.118536
2016-08-18 01:34:40.493 test background GPS from StackOF[2285:747132] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 355.141209
2016-08-18 01:34:40.496 test background GPS from StackOF[2285:747132] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 355.147748
2016-08-18 01:34:40.505 test background GPS from StackOF[2285:747132] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 355.148158
2016-08-18 01:34:40.507 test background GPS from StackOF[2285:747132] Lat/Long: <redacted GPS coordinates>, horizontal accuracy: 65.000000
2016-08-18 01:34:41.640 test background GPS from StackOF[2285:747132] inner block [[UIApplication sharedApplication] endBackgroundTask:taskIdentifier];
2016-08-18 01:34:41.642 test background GPS from StackOF[2285:747132] inner block start updating location, _locationManager.allowsBackgroundLocationUpdates == YES
/****
stops polling...
****/
注意剩下的背景时间。如果应用程序在CLLocationManager开始更新位置之前进入后台,则剩下的背景时间为3分钟。我不太确定自己,因为我也有我自己的全球定位系统的背景运行问题,但从这个简短的测试,似乎CLLocationManager必须被调用之前,应用程序进入后台。
下面是我把NSLogs放在这里的地方:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"viewDidLoad");
_locationManager = [[CLLocationManager alloc] init];
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_8_3)
{
_locationManager.allowsBackgroundLocationUpdates = YES;
NSLog(@"_locationManager.allowsBackgroundLocationUpdates = YES;");
}
_locationManager.delegate = self;
_locationManager.activityType = CLActivityTypeOther;
_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
_locationManager.distanceFilter = kCLDistanceFilterNone;
[_locationManager requestAlwaysAuthorization];
NSLog(@"_locationManager requestAlwaysAuthorization");
// Creating a background task to delay the start of the startUpdatingLocation
__block UIBackgroundTaskIdentifier taskIdentifier = UIBackgroundTaskInvalid;
taskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:taskIdentifier];
NSLog(@"[[UIApplication sharedApplication] endBackgroundTask:taskIdentifier];");
}];
// Delaying the call to startUpdatingLocation 10 seconds, enough time to dismiss the application
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[_locationManager startUpdatingLocation];
NSLog(@"[_locationManager startUpdatingLocation];");
if(_locationManager.allowsBackgroundLocationUpdates == YES)
{
NSLog(@"start updating location, _locationManager.allowsBackgroundLocationUpdates == YES");
}
else
{
NSLog(@"start updating location, _locationManager.allowsBackgroundLocationUpdates == NO");
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] endBackgroundTask:taskIdentifier];
NSLog(@"inner block [[UIApplication sharedApplication] endBackgroundTask:taskIdentifier];");
if(_locationManager.allowsBackgroundLocationUpdates == YES)
{
NSLog(@"inner block start updating location, _locationManager.allowsBackgroundLocationUpdates == YES");
}
else
{
NSLog(@"inner block start updating location, _locationManager.allowsBackgroundLocationUpdates == NO");
}
});
});
}
CLLocationManager
委托函数:
-(void)locationManager:(CLLocationManager *) manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations
{
CLLocation *location = [locations firstObject];
NSLog(@"Lat/Long: %f %f, horizontal accuracy: %f",
location.coordinate.latitude,
location.coordinate.longitude,
location.horizontalAccuracy);
}
AppDelegate:
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"applicationDidEnterBackground. Background time left: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]);
}
TL;博士:尝试在viewDidLoad
或AppDelegate application:didFinishLaunchingWithOptions:
中调用startUpdatingLocation
https://stackoverflow.com/questions/37045746
复制