正如所讨论的,我使用了替代解决方案,这里是:
在 viewDidLoad 或 didFinishingLaunchingWithOptions 下添加:
locationsLogged = [[NSMutableArray alloc] init];
lastLocationTime = [[NSDate alloc] init]
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
在 applicationDidEnterBackground 添加:
[locationManager startMonitoringSignificantLocationChanges];
添加:
-(void) storeLocations:(CLLocation *)location
{
bgTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:
^{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
}];
[locationsLogged addObject:location];
if (bgTask != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
}
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
BOOL isInBackground = NO;
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
{
isInBackground = YES;
}
NSDate *now = [NSDate date];
NSTimeInterval diff = [now timeIntervalSinceDate:lastLocationTime];
if ((int)diff > 600) {
if (isInBackground)
{
[lastLocationTime release];
lastLocationTime = [[NSDate alloc] init];
[self storeLocations:newLocation];
}
else
{
// ...
}
}
}
然后在 applicationDidBecomeActive 上,您可以使用应用位置信息所需的任何逻辑。