一段时间后CLLocationManager didUpdateToLocation不会被调用

我试图logging一段时间的用户位置。 如果用户在移动它工作正常,委托方法didUpdateToLocation调用可靠。然而,如果用户是静止的,应用程序在后台运行,那么一段时间后,不再调用委托方法。 要重新启动它,应用程序需要被购买到前台。 一旦它被激活,委托方法被再次可靠地调用。

我最初认为这可能是由于CLLocationManager对象是在ViewController中声明的,所以我将其更改为在AppDelegate中声明,但这也没有帮助。

我也尝试了distanceFilter属性无济于事。 我目前正在使用View控制器中的以下代码进行设置。 请注意,对象本身是在AppDelegate对象中声明和初始化的。

app.locationManager.delegate = self; app.locationManager.desiredAccuracy = kCLLocationAccuracyBest; app.locationManager.distanceFilter = kCLDistanceFilterNone; [app.locationManager startUpdatingLocation]; 

有没有人遇到这个问题? 任何指针将不胜感激。 现在我已经挣扎了好几天了。

iOS 6引入了CLLocationManager属性pausesLocationUpdatesAutomatically。 在设置CLLocationManager时,需要将其设置为NO,如下所述: http : //www.stackoverflow.com/a/12781634/700769

在UpdateLocation方法中添加此代码

 - (void) updateLocation{ self.locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.pausesLocationUpdatesAutomatically = NO; locationManager.desiredAccuracy = kCLLocationAccuracyBest; if ([CLLocationManager locationServicesEnabled]) { [locationManager startUpdatingLocation]; } else { NSLog(@"Location services is not enabled"); } } 

也可以在scheme中编辑设置:scheme/编辑scheme/选项/允许位置模拟检查,但没有设置默认位置。

您需要在应用程序plist文件的UIBackgroundModes中添加位置。

 if #available(iOS 9.0, *){ locationManager.allowsBackgroundLocationUpdates = true; }