Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not being prompted to enable Location Services in App

UPDATE: THIS IS NOT A DUPLICATE. I have already added the required key to the info.plist as stated in my original question and the issue remains. I have tried all three keys in various combinations.

Before anyone gets upset, I have read through many Apple Dev forum posts and stack overflow posts and cannot figure out why my app refuses to prompt the user to allow When In Use authorization.

I've added the following key to my Info.plist file with an accompanying String value:

NSLocationWhenInUseUsageDescription

I've then written (both in Swift and Obj-C) the code that should prompt the user:

@property CLLocationManager *location;
...
@synthesize location;
...
location = [[CLLocationManager alloc] init];
location.delegate = self;

location.desiredAccuracy = kCLLocationAccuracyBest;
location.distanceFilter = kCLDistanceFilterNone;

[location requestWhenInUseAuthorization];
[location startUpdatingLocation];


I'm using the following CLLocationManagerDelegate methods.
- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
- (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

This was basically copied straight from the Apple "LocateMe" sample code.

No matter what various sequences or minor changes I try, the app never prompts to allow authorization. I've used a switch statement to determine the state of [CLLocationManager authorizationStatus], but continually recieve a "Not Determined" response.

if ([CLLocationManager locationServicesEnabled]) {
        switch ([CLLocationManager authorizationStatus]) {
            case kCLAuthorizationStatusAuthorizedAlways:
                NSLog(@"Always Authorized");
                break;
            case kCLAuthorizationStatusDenied:
                NSLog(@"Denied");
                break;
            case kCLAuthorizationStatusAuthorizedWhenInUse:
                NSLog(@"Authorized in Use");
                break;
            case kCLAuthorizationStatusNotDetermined:
                NSLog(@"Not Determined");
                break;
            case kCLAuthorizationStatusRestricted:
                NSLog(@"Restricted");
                break;
        }

    }

Any help would be greatly appreciated. I'm running Xcode 6.2 (6C101) with iOS 8.1.2 physical device and iOS 8.2 (12D5452a) simulator for testing.

like image 328
Andrew Avatar asked Dec 29 '14 18:12

Andrew


4 Answers

I faced the exact same problem, after digging in for two days, it turned out that the app does not read from the info.plist file that is included in the bundle in project navigator's pane, because it has been localized to another language and base internationalization is being used, that means in Finder there will be 3 info.plist files:

1- under Tests folder

2- under base.lproj folder

3- under ar.lprj (ar refers to Arabic language).

the project reads from base.lproj's version which is not included inside the bundle in the project navigator's pane.

what I did is that I took a backup copy of the plist file that I want (number 2 above), and removed localization on the info.plist file that is inside project navigator and chose delete from disk to delete it completely, then I took the back up copy and placed in under project's root in Finder and import it back in Xcode, and now the project reads from the new info.plist file and the key NSLocationWhenInUseUsageDescription fires up the user authorization alert at app launch.

hope this helps.

like image 76
JAHelia Avatar answered Oct 04 '22 13:10

JAHelia


If you have changed the Location Settings in your iPhone simulator this may cause the prompt to not show. For example, if you are already set the Location permissions via Allow Locations access iOS prompt and made a decision this is stored in the Privacy settings for the iPhone simulator and so subsequent calls to locationManager.requestAlwaysAuthorization() or locationManager.requestWhenInUseAuthorization() will not display the iOS prompt because you already have defined your Privacy -> Location Settings for your app.

To clear settings for an iPhone simulator: "iOS Simulator" -> "Reset Content and Settings..."

Also I did a Clean build in Xcode and quit and restarted the iPhone simulator at some point, I could be wrong about this part being of any help...

like image 29
Brian Ogden Avatar answered Oct 04 '22 14:10

Brian Ogden


Lots of useful comparisons here: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

Also, silly question time: you mix _location and self.location but you don't show your property/synthesize code. Are you sure you're operating on the same object?

like image 22
Brad Brighton Avatar answered Oct 04 '22 14:10

Brad Brighton


I found that in your info.plst file you can't use the Privacy - Location Usage Description key, you have to have a key called NSLocationWhenInUseUsageDescription and a value. I was able to get your code working by adding this key into my info.plist

enter image description here

#import <MapKit/MapKit.h>

@interface ViewController ()

@property CLLocationManager * location;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _location = [[CLLocationManager alloc] init];

    self.location.delegate = self;
    self.location.desiredAccuracy = kCLLocationAccuracyBest;
    self.location.distanceFilter = kCLDistanceFilterNone;

    [_location requestWhenInUseAuthorization];
    [self.location startUpdatingLocation];
}
like image 20
ecnepsnai Avatar answered Oct 04 '22 15:10

ecnepsnai