samiles
samiles

Reputation: 3900

Finding out if location services are enabled is not working

I have an app which uses the device location. If they allow the location, I want to run my method getDataFromJson and run my app as normal. If they deny it, or have denied it before, I wish to show them a view explaining they need to go to settings and allow it.

I have a lot of code, but it doesn't work at the moment. Can anyone help explain where the problem is?

Many thanks!

- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([CLLocationManager authorizationStatus] == YES) {
        //are enabled, run the JSON request
        [self getDataFromJson];
    } else {
        //is not enabled, so set it up
        NSLog(@"no");
        [locationManager location];
    };

}

-(CLLocationCoordinate2D) getLocation{

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];
    return coordinate;

}

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status == kCLAuthorizationStatusDenied) {
        //location denied, handle accordingly
        locationFailView.hidden = NO;
        mainView.hidden = YES;
    }
    else if (status == kCLAuthorizationStatusAuthorized) {
        //hooray! begin tracking
        [self getDataFromJson];
    }
}

//class to convert JSON to NSData
- (IBAction)getDataFromJson {
    CLLocationCoordinate2D coordinate = [self getLocation];
    NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
    NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
    ...
}

Upvotes: 0

Views: 319

Answers (1)

Anton
Anton

Reputation: 2847

+ (CLAuthorizationStatus)authorizationStatus

Return Value A value indicating whether the application is authorized to use location services.

Discussion The authorization status of a given application is managed by the system and determined by several factors. Applications must be explicitly authorized to use location services by the user and location services must themselves currently be enabled for the system. This authorization takes place automatically when your application first attempts to use location services.

+ (BOOL)locationServicesEnabled

Returns a Boolean value indicating whether location services are enabled on the device.

You may check this two state: locationServicesEnabled and authorizationStatus then decide which method should use.

AuthorizationStatus should check with states:

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

but you check equal with bool value in viewDidLoad method.

Upvotes: 3

Related Questions