Search code examples
iosobjective-cbooleancllocationmanagernslog

BOOL method is returning true when used by NSLog when it should be returning false


This is a location app. When I first run the app on my iPhone, an alert window asks me if I would like to allow location services. After selecting OK or Don't allow I can then tap a button that is setup to display my current location. The button is also setup to print an NSLog to the console with the value of a BOOL function called "locationServicesEnabled" and returns yes/1 if they are and no/0 if they are not.

My problem is that when I select the Don't Allow option, and I press the button, the NSLog still prints the value of 1 to the console which is incorrect. It should be 0 or false. A text label that is connected to my button even says "Your location is null", but for some reason the NSLog always shows a BOOL value of 1.

Here is my ViewController.m code:

#import "ViewController.h" 
#import <CoreLocation/CoreLocation.h> 

@interface ViewController () <CLLocationManagerDelegate>

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;

@end

@implementation ViewController

- (void)viewDidLoad
{

[super viewDidLoad];

self.gpsLM = [[CLLocationManager alloc]init];

[self.gpsLM startUpdatingLocation]; 

}

- (void)didReceiveMemoryWarning

{
[super didReceiveMemoryWarning];
}


-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{


}

-(IBAction)gpsButton{

CLLocation * currentLocation = self.gpsLM.location;

self.gpsLabel.text = [NSString stringWithFormat:@"Your Location is %@", currentLocation];

NSLog(@"Location services enabled: %hhd",[CLLocationManager locationServicesEnabled]);

}

@end

Solution

  • You need to check [CLLocationManager authorizationStatus], not locationServicesEnabled. Authorization status returns an app-specific value, one of the following:

    typedef enum {
       kCLAuthorizationStatusNotDetermined = 0,
       kCLAuthorizationStatusRestricted,
       kCLAuthorizationStatusDenied,
       kCLAuthorizationStatusAuthorized
    } CLAuthorizationStatus;
    

    The names are hopefully self-explanatory.