DidUpdateLocations not being called
I have my object set as a delegate to locationManager, and the didChangeAuthorizationStatus method is called, which does this:
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.AuthorizedWhenInUse {
self.locationManager.startUpdatingLocation()
}
}
I also have this method which is never called like this:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if locations.count == 0 {
return
}
//Do stuff
}
Any thoughts on why this might not be caused? I suppose the freed object is an option, but then it will also be freed by the time it gets into the authorizationStatus method.
It's already late for an answer, but I stumbled upon this when I had the same problem ( didChangeAuthorizationStatus
was being called but not didUpdateLocations
). Well of course this is not something unrelated to code, but rather I tested in a simulator that had no location. So no location was found, the result is didUpdateLocations
never called.
To fix this ... in the simulator go to Debug -> Location-> <choose location>
.
EDIT It should also be noted that locationManager:didFailWithError:
will run if no location is set in the simulator, as you would expect.
It works in iOS 10 and iOS 11 .
Create CLLocationManager property with strong and add CLLocationManagerDelegate
@property (strong, nonatomic) CLLocationManager *locationManager;
Add below properties to your info.plist file
Privacy - Location When In Use Usage Description
Privacy - Location Always Usage Description
Privacy - Location Usage Description
Add below method to your .m file
-(void)getCurrentLocation
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
self.locationManager.distanceFilter=kCLDistanceFilterNone;
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startMonitoringSignificantLocationChanges];
[self.locationManager startUpdatingLocation];
}
and just calling the [self getCurrentLocation]
method .
Here is a snippet from my application:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
...
locationManager.delegate = self
locationManager.activityType = CLActivityType.Fitness
locationManager.distanceFilter = 10 // 10m
locationManager.requestAlwaysAuthorization()
// get current location
if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized {
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .Authorized {
locationManager.startUpdatingLocation()
}
}
You must put the key NSLocationAlwaysUsageDescription
in yours Info.plist
and set a value for the authorization question. This requirement applies to iOS 8.