문제

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
도움이 되었습니까?

해결책

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.

다른 팁

I just figured out the answer to my own question. The bool function will only return a false value if the iPhone's location services option is disabled entirely. If you only have 1 single app disabled, but have the main feature enabled and have other app's using it, then it will still return a value of 1. It looks like the locationServicesEnabled method is device specific and not app specific.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top