0

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??

3 Answers3

1

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

Dean Davids
  • 4,174
  • 2
  • 30
  • 44
  • Thank for your reply. I also add string "This will be used to obtain or track your location." for this key.But permission alert not shown. – Dhvl Golakiya Oct 30 '14 at 13:14
  • I had this experience on update to iOS8 for working app. Adding that string is all I did to fix. I don't think the system asked for permission immediately either, eventually it did. In any case, installed app regained locations right away with no other changes. – Dean Davids Oct 30 '14 at 13:30
1

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

xRab
  • 133
  • 1
  • 4
  • I'm getting "Location services are not enabled" in Log. – Dhvl Golakiya Oct 30 '14 at 13:42
  • This is going to be called on change of status. So, when the user goes and changes permission setting after having dismissed the initial request. – Dean Davids Oct 30 '14 at 14:07
  • If its kCLAuthorizationStatusNotDetermined, You can remove the app and try again. Reference of kCLAuthorizationStatusNotDetermined https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/c/tdef/CLAuthorizationStatus OR check you iPhone Settings for Location Service Permission – xRab Oct 30 '14 at 14:09
0

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.

  • The use of the `IS_OS_8_OR_LATER` macro is not appropriate. In fact, it's pointless since you are properly using `respondsToSelector`. – rmaddy Apr 28 '16 at 17:25