I want to get current location of device. Code works fine normally. It gives location if user has not changed app authorization status for location service. I am also able to check if user has denied permission for location service.
Issue is when user deauthorizes the app to use location service and then authorizes again. In this case, after this if I try to get location it gives nil
though it called
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
delegate method with status 3
i.e. kCLAuthorizationStatusAuthorized
.
Code to get current location :
CLLocation * location = self.locationManager.location;
Getter method :
- (CLLocationManager *)locationManager
{
if (!locationManager)
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
}
return locationManager;
}
CLLocationManager delegate method :
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
DLog(@"Location authorization changed : %d", status);
// If user has denied permission for location service
if (status == kCLAuthorizationStatusDenied)
{
DLog(@"Location service denied.");
// If authorization status changed method is already called, then SDK will not call again on same object.
// Thus, set CLLocationManager object to nil so that next time we try to get location, it will create a new object,
// and that will send message about authorization status changed.
self.locationManager.delegate = nil;
self.locationManager = nil;
}
else if (status == kCLAuthorizationStatusNotDetermined)
{
// If authorization status changed method is already called, then SDK will not call again on same object.
// Thus, set CLLocationManager object to nil so that next time we try to get location, it will create a new object,
// and that will send message about authorization status changed.
self.locationManager.delegate = nil;
self.locationManager = nil;
}
else if (status == kCLAuthorizationStatusAuthorized)
{
}
}
Any idea about this?
self.locationManager.location
is nil
since you never started updating the location.
In the apple docs it is stated about the location
property of the locationManager:
The value of this property is nil if no location data has ever been retrieved.
For this reason you need to somehow update your iPhones location!
Usually this means you want to call
[self.locationManager startUpdatingLocation]
but you can also use
[self.locationManager startMonitoringSignificantLocationChanges]