Not being prompted to enable Location Services in App
Asked Answered
K

5

7

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.

Kapok answered 29/12, 2014 at 18:19 Comment(1)
@n00bProgrammer I don't see that reset option available in the Settings > Reset tab of the simulator. Also, I've written two programs, one in Swift and one in Obj-C and both do the same thing. I've also tested with a physical device and got the same results. I've not tried resetting the hardware yet.Kapok
I
3

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

Interdependent answered 14/3, 2016 at 1:22 Comment(1)
Now it's little changed "iOS Simulator" -> Device -> "Erase All Content Settings.."Fitts
P
2

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.

Paction answered 19/3, 2015 at 8:17 Comment(0)
C
1

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];
}
Cancellate answered 29/12, 2014 at 18:54 Comment(2)
However I'm testing on 8.1, not 8.2.Cancellate
I've tested this using the three different keys available in various ways hoping to get it to work -- at one point, one of my projects magically started working, but I can't replicate those changes to this simple project (who's sole purpose is this test) to make it prompt.Kapok
B
1

It may be silly, but i just spent almost 2 hours double and triple checking all of the answers i could find about CLLocationManager permissions.

I felt stupid when i finally realized that i had 2 info.plist files and i was using the wrong one!

You can check in your project's build settings - there's a value called Info.plist File.

Just want to leave it here for future readers.

Bedlam answered 29/3, 2015 at 13:21 Comment(0)
H
0

I wasted my 1-2 days behind this. Updating only from info.plist didn't work in my case.

Instead if u navigate to TARGETS -> YOUR_APP_NAME -> Info section

You can see Custom iOS Target Properties, here you can see the purpose strings. Update your purpose string for location, contacts, notifications and many more. Make sure the permissions are visible in info.plist too.

Hedelman answered 22/12, 2023 at 22:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.