Question

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?

Was it helpful?

Solution

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!

Apple Docs CLLocationManager

Usually this means you want to call

[self.locationManager startUpdatingLocation]

but you can also use

[self.locationManager startMonitoringSignificantLocationChanges]

OTHER TIPS

If you set the delegate to nil you will get no updates about authorization status changes, isn't it?

self.locationManager.delegate = nil;

I think you should keep the delegate in order to receive authorization status updates and then call the startUpdatingLocation method in order to get the current location.

- (void)startUpdatingLocation
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top