2

I have found the following solution explaining how to utilize location services in iOS 8. Unfortunately I have got only an iPhone 6 simulator and not the real device. I tried to add this code in the viewDidLoad method but the authorization dialog is never shown. Why is this?

I added a breakpoint in the following, it gets executed but does not show the dialouge..:

    // Will open an confirm dialog to get user's approval
    [locationManager requestAlwaysAuthorization];

And here is the full viewDidLoad method:

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Initialize location manager
locationManager = [CLLocationManager new];
locationManager.delegate = self;

float system = [[[UIDevice currentDevice] systemVersion] floatValue ];

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 &&
    [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways
    ) {
    // Will open an confirm dialog to get user's approval
    [locationManager requestAlwaysAuthorization];
} else {
    [locationManager startUpdatingLocation]; //Will update location immediately
}
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

PS: I also modified the info.plist as following:

enter image description here

PPS:

This is how I implement (in the same ViewController) the rest of the CLLocationManagerDelegate protocol:

#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
        case kCLAuthorizationStatusNotDetermined: {
            NSLog(@"User still thinking..");
        } break;
        case kCLAuthorizationStatusDenied: {
            NSLog(@"User dislikes your app");
        } break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
        case kCLAuthorizationStatusAuthorizedAlways: {
            [locationManager startUpdatingLocation];
        } break;
        default:
            break;
    }
}

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations {
    CLLocation *location = [locations lastObject];
    NSLog(@"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude);
}

EDIT 2: I added the following block of code as suggested in an answer. The code is executed but dialogue fails to appear.

#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
        case kCLAuthorizationStatusNotDetermined: {
            if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
                [locationManager requestAlwaysAuthorization];
            }
        } break;
        case kCLAuthorizationStatusDenied: {
            NSLog(@"User dislikes your app");
        } break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
        case kCLAuthorizationStatusAuthorizedAlways: {
            [locationManager startUpdatingLocation];
        } break;
        default:
            break;
    }
}
Community
  • 1
  • 1
mm24
  • 9,280
  • 12
  • 75
  • 170
  • Would you check the `AuthorizationStatus` from `locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:` – Mustafa Ibrahim Jan 26 '15 at 12:08
  • yes.. thanks for the comment.. I added a PPS: in the question.. does this help to explain? – mm24 Jan 26 '15 at 12:10
  • Check for AuthorizationStatus value and [locationManager requestAlwaysAuthorization] for kCLAuthorizationStatusNotDetermined and kCLAuthorizationStatusDenied values. – Mustafa Ibrahim Jan 26 '15 at 12:14
  • It does hit kCLAuthorizationStatusNotDetermined – mm24 Jan 26 '15 at 12:17
  • I guess is because the authorization dialogue gets never displayed.. – mm24 Jan 26 '15 at 12:17
  • Location tracking doesn't need a real device. So I'd suggest you to try on iOS 8.1 (latest public version, non beta). Also note that you can `startUpdatingLocation` even before the user authorised the app: you simply won't get the data, but if you start updating early, when the user authorises you'll likely get location informations immediately (better UX) – Alessandro Vendruscolo Jan 26 '15 at 12:51
  • Also, did you link against `CoreLocation`? – Alessandro Vendruscolo Jan 26 '15 at 12:53

2 Answers2

0

Add this to the didChangeAuthorizationStatus:

case kCLAuthorizationStatusNotDetermined: {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
        [locationManager requestAlwaysAuthorization];
    }

EDIT

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 &&
    [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways
    ) {
    // Will open an confirm dialog to get user's approval
        [locationManager requestAlwaysAuthorization];
    }

    [locationManager startUpdatingLocation]; //Will update location immediately
CW0007007
  • 5,681
  • 4
  • 26
  • 31
  • Thanks! I added it but the dialogue still fails to appear. Will add EDIT on question. – mm24 Jan 26 '15 at 12:26
  • Your plist has "Location is required..." try taking out the ". It probably isn't an issue but may be. – CW0007007 Jan 26 '15 at 12:30
  • yep.. I took it away but still doesn't work.. is it pheraphs a bug with XCode 6.2 beta? – mm24 Jan 26 '15 at 12:37
  • ah no, you request permission but you don't actually start the location services ? See my Edit: – CW0007007 Jan 26 '15 at 12:41
  • DOn't think it will ask until you actually try and start the service – CW0007007 Jan 26 '15 at 12:42
  • I removed that but it doesn't ask.. only on my real iOS 7 device (iPhone 4 for the records).. – mm24 Jan 26 '15 at 12:45
  • @CW007007 I upvoted but will wait to accept as I have not been able to resolve the issue. Thanks for the contribution in the meanwhile. – mm24 Feb 13 '15 at 01:11
  • Just re-read the question, have you chosen a location for the simulator ? By defualt it has no location. – CW0007007 Feb 13 '15 at 07:35
  • Do not check the `systemVersion`. There are proper ways to check if an API exists or not. – rmaddy Apr 28 '16 at 17:30
0

I think your code might not have any problem. Here is the code that I am using:-

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

    if(IS_OS_8_OR_LATER) {
        [locationManager requestAlwaysAuthorization];
    }

    [locationManager startUpdatingLocation];

You might not understand how the authorization dialog works. For your information, the authorization dialog will only appear once (Either you allow it or disallow it). Afterwards, you can change the authorization from the Settings.

If you want the authorization dialog to re-appear, you should delete the app from you iPhone Simulator (Long press the app and tap the X button). When you relaunch the app from XCode into the iPhone Simulator, then only the authorization dialog will re-appear.

Ricky
  • 10,485
  • 6
  • 36
  • 49
  • I upvoted but will wait to accept as I have not been able to resolve the issue. Thanks for the contribution in the meanwhile. – mm24 Feb 13 '15 at 01:11
  • Do not use IS_OS_8_OR_LATER. There are proper ways to check if an API exists. – rmaddy Apr 28 '16 at 17:30