Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for iOS Location Services

Tags:

I have a view with a map and a button (like the Maps app once) that allows the user to center and zoom his current location on the map. If I can not use the locationServicesEnabled method (always returns YES), should I create a BOOL attribute to check if the didFailWithError method is called and know if I can call the button method?

Thanks for reading.

Edited:

This code does not work for me. I am using the simulator. I am always getting YES when asking locationServicesEnabled.

// Gets the user present location.
- (IBAction)locateUser:(id)sender {

    if([CLLocationManager locationServicesEnabled]) {

        CLLocationCoordinate2D coordinate;

        coordinate.latitude = self.mapView.userLocation.location.coordinate.latitude;
        coordinate.longitude = self.mapView.userLocation.location.coordinate.longitude;

        [self zoomCoordinate:coordinate];
    } else {
        [[[[UIAlertView alloc] initWithTitle:@"Warning." message:@"Location services are disabled." 
                                    delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease] show];     
    }
}
like image 299
Daniel García Baena Avatar asked Nov 30 '10 21:11

Daniel García Baena


People also ask

How do you check if Location Services are enabled iOS?

Users can enable or disable location services by toggling the Location Services switch in Settings > Privacy.

How do I know if Location Services are on?

Open your phone's Settings app. Under "Personal," tap Location access. At the top of the screen, turn Access to my location on or off.


2 Answers

In Preferences you have two options to disable the location services. The first option is a global switch to disable the location service for all apps "[CLLocationManager locationServicesEnabled]". The second option let you disable the location service for some apps but not for all apps.

To check if its disabled globally and if its disabled for your app use following:

if([CLLocationManager locationServicesEnabled] && 
   [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
...
}
like image 53
Robin Avatar answered Oct 04 '22 01:10

Robin


"locationServicesEnabled" checks if the user has enabled Location Services in Preferences. Your MapView probably checks this value already and should not set any values to "self.mapView.userLocation" if Location Services are not available. This SO question might give you some more info.

like image 25
Nippysaurus Avatar answered Oct 04 '22 02:10

Nippysaurus