Dhvl Golakiya
Dhvl Golakiya

Reputation: 53

CoreLocation not working in ios 8

I'm using Location service in my app.

But [CLLocationManager authorizationStatus] is kCLAuthorizationStatusNotDetermined both before [self.locationManager startUpdatingLocation];

code of project:

Implementation

#import <CoreLocation/CoreLocation.h> 
@interface NewRunViewController () <CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self startLocationUpdates];
}

- (void)startLocationUpdates
{
    // Create the location manager if this object does not
    // already have one.
    if (self.locationManager == nil) {
        self.locationManager = [[CLLocationManager alloc] init];
    }

    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.distanceFilter = 10;

    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
    }
    [self.locationManager startUpdatingLocation];
}

I also add NSLocationWhenInUseUsageDescription key into info.plist file. And app also does not ask for user permission to use core location service. Can anyone help me??

Upvotes: 0

Views: 188

Answers (3)

Dhvl Golakiya
Dhvl Golakiya

Reputation: 53

Thank you guys for your answers. I found solution of problem. I checked the version of system like:

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

in startLocationUpdates method:

if(IS_OS_8_OR_LATER) {
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.locationManager requestAlwaysAuthorization];
    }
}

Now it's working properly.

Upvotes: 0

xRab
xRab

Reputation: 133

you have to move

[self.locationManager startUpdatingLocation];

to the delegate method

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
        {
            NSLog(@"User still thinking");
        }
        break;
        case kCLAuthorizationStatusDenied:
        {
            NSLog(@"User denied location request");
        }
            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
        case kCLAuthorizationStatusAuthorizedAlways:
        {
            [self.locationManager startUpdatingLocation];
        }

            break;
        default:
            break;
    }
}

Update : Make the log easier to be understood

Upvotes: 1

Dean Davids
Dean Davids

Reputation: 4214

You must include a string value for the key NSLocationAlwaysUsageDescription for your target.

Look on the Info page of your target in Xcode. The string will be the text for the alert when the system asks the user for permission to use location services in your app.

This is my own code for handling same. It is requesting Always Authorization but you could replace with When in Use.

  CLAuthorizationStatus auth = [CLLocationManager authorizationStatus];
  if (auth == kCLAuthorizationStatusNotDetermined && [self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
      [self.locationManager performSelector:@selector(requestAlwaysAuthorization) withObject:NULL];
  } else {
      if (auth == kCLAuthorizationStatusAuthorizedAlways) {
          [self notificationsSetup];
      } else {
          NSLog(@"Device is not authorized for proper use of the location Services, no logging will be performed");
      }
  }

You might find my github repository helpful- TTLocationHandler on Githup

Upvotes: 1

Related Questions