CLLocationManager didUpdateToLocation not being called after some time

一世执手 提交于 2020-01-24 06:07:09

问题


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.


回答1:


iOS 6 introduces the CLLocationManager property pausesLocationUpdatesAutomatically. It needs to be set to NO when you set up your CLLocationManager, as described here: http://www.stackoverflow.com/a/12781634/700769




回答2:


Add this code in UpdateLocation method

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

also edit setting in Schemes: Scheme/Edit Scheme/Options/Allow Location Simulation checked but don't have a default location set.




回答3:


You need to add location in UIBackgroundModes at app plist file.




回答4:


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


来源:https://stackoverflow.com/questions/14503161/cllocationmanager-didupdatetolocation-not-being-called-after-some-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!