我有一个应用程序,可以在后台进行位置跟踪。一旦我收到WhatsApp或手机,我的位置就会停止并在看到图像后恢复。有人会有关于这个问题的信息吗?
- (void) startUpdatingLocationFonction
{
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
[self.locationManager startUpdatingLocation];
self.locationManagerStartDate = [NSDate date];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[AppDelegate sharedInstance].latitude=newLocation.coordinate.latitude;
[AppDelegate sharedInstance].longitude=newLocation.coordinate.longitude;
if (![[FoncGlobal sharedFoncGlobal] isValidLocation:(CLLocation *)newLocation withOldLocation:(CLLocation *)oldLocation])
return;
if ((oldLocation.coordinate.longitude != newLocation.coordinate.longitude)
|| (oldLocation.coordinate.latitude != newLocation.coordinate.latitude))
{
[[FoncGlobal sharedFoncGlobal] upDateTraceUser: [NSString stringWithFormat:@"%.8lf",newLocation.coordinate.latitude]: [NSString stringWithFormat:@"%.8lf",newLocation.coordinate.longitude] :[NSString stringWithFormat:@"%.8lf",newLocation.altitude]:[NSString stringWithFormat:@"%.8lf",newLocation.speed]:0];
}
}
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
if (_inBackground) {
[self extendBackgroundRunningTime];
}
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self extendBackgroundRunningTime];
_inBackground = YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
_inBackground = NO;
}
- (void)extendBackgroundRunningTime {
if (bgTask != UIBackgroundTaskInvalid) {
return;
}
NSLog(@"Attempting to extend background running time");
__block Boolean self_terminate = NO;
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"DummyTask" expirationHandler:^{
NSLog(@"Background task expired by iOS");
if (self_terminate) {
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (true) {
if ([AppDelegate sharedInstance].startTrack)
[self startUpdatingLocationFonction];
[NSThread sleepForTimeInterval:3];
}
});
}