JLau_cy
JLau_cy

Reputation: 715

How to handle Enable Location Services after a user selects allow?

Basically when my app launches for the first time the Enable Location Services prompt appears. When the user taps on Allow I would like to start updating user location and zoom into the region.

In my viewDidLoad I start the location manager, but unfortunately the view is loaded before the user has a chance to tap on Allow. Everything works fine on second launch of the application because the user would have allowed location services already

My question is how can I capture the event of tapping on Allow so I can run the code to zoom into a region?

I have tried using -(void)locationManager:didChangeAuthorizationStatus: but it doesn't seem to call this delegate method when the user taps on allow.

Hope this makes sense I am very new to this.

Upvotes: 2

Views: 3248

Answers (3)

Gaurav Taywade
Gaurav Taywade

Reputation: 8347

You can handle it like this :

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{

    switch([CLLocationManager authorizationStatus])
    {
        case kCLAuthorizationStatusAuthorized:
            NSLog(@"Location services authorised by user");
            break;

        case kCLAuthorizationStatusDenied:
            NSLog(@"Location services denied by user");
            break;

        case kCLAuthorizationStatusRestricted:
            NSLog(@"Parental controls restrict location services");
            break;

        case kCLAuthorizationStatusNotDetermined:
            NSLog(@"Unable to determine, possibly not available");
            break;
    }
}

Upvotes: 1

Leandro Alves
Leandro Alves

Reputation: 2318

Here it works just fine. I initiate the location manager, then I set it's delegate and start it. When the popup to allow comes up, the -(void)locationManager:didChangeAuthorizationStatus: is called with CLAuthorizationStatus equals to kCLAuthorizationStatusNotDetermined. If I tap "Dont' Allow", it's called again with CLAuthorizationStatus equals to kCLAuthorizationStatusDenied. When tapping "Allow", it's called with CLAuthorizationStatus equals to kCLAuthorizationStatusAuthorized. Check if your delegate is set correctly.

Upvotes: 1

Yarlik
Yarlik

Reputation: 216

To my knowledge you cannot, but you don't have to capture this event, because you won't be able to zoom to the certain location before you get the coordinates of this location. Your app works fine on the second launch, because it uses cached location data from the first start. So, what you need is to run your zooming code after you received your new valid coordinates. If you use CLLocationManager, than look at

– locationManager:didUpdateToLocation:fromLocation: 

in its delegate. If user denied to use location services, that your delegate will receive

locationManager:didFailWithError:

With corresponding error.

If you use MKMapKit, than in MKMapView delegate implement method

– mapViewWillStartLocatingUser: 

to focus on current user position. to handle denial implement

– mapView:didFailToLocateUserWithError:

Links to corresponding Apple documentation:

CLLocationManager

CLLocationManagerDelegate

MKMapViewDelegate

Upvotes: 5

Related Questions