我在Xcode上用Objective C编程,当我的应用程序进入后台时似乎出现了问题。我有一个GPS跟踪应用程序,当用户运行该应用程序时它运行良好,但是当它在后台运行时,GPS不会逐步跟踪用户的位置。它会暂停,然后在应用程序重新打开时自动找到该位置,但是我希望GPS一直运行,这样它就不会像上图中那样“走开” 1。
我已经在后台模式下打开了位置更新和后台抓取功能。
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations: (NSArray *)locations
{
if (!self.timerPause) {
for (CLLocation *newLocation in locations) {
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (fabs(howRecent) < 10.0 && newLocation.horizontalAccuracy < 20)
其他代码
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc] init];
}
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.activityType = CLActivityTypeAutomotiveNavigation;
self.locationManager.distanceFilter = 10; // meters
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
locationManager.allowsBackgroundLocationUpdates = YES;
将其添加到您设置了位置管理器的位置。它应该工作:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句