4

I am trying to record a users location over time. If the user is on the move it works fine and the delegate method didUpdateToLocation is invoked reliably.However if the user is stationary and the app is running in the background then after some time, the delegate method is no longer invoked. To restart it, the app needs to be bought into the foreground. Once it is active the delegate method is invoked reliably again.

I initially thought that this could be due to the fact that the CLLocationManager object was declared within a ViewController, so I changed it to be declared within the AppDelegate but that did not help either.

I have also experimented with the distanceFilter property to no avail. I am currently setting it up using the following code from within a View controller. Note that the object itself is declared and initialized in the AppDelegate object.

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

Has anyone else run into this issue? Any pointers would be appreciated. I have been struggling with this for a few days now.

4

4 回答 4

5

在 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");
    }
}

还在方案中编辑设置:方案/编辑方案/选项/允许位置模拟已选中,但没有设置默认位置。

于 2013-06-18T08:46:23.830 回答
5

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

于 2013-01-25T09:24:45.033 回答
4

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

于 2013-01-24T14:05:15.517 回答
1
if #available(iOS 9.0, *){
    locationManager.allowsBackgroundLocationUpdates = true;
}
于 2016-05-05T22:07:11.920 回答