iOS 8 requestWhenInUseAuthorization no Popup

39,244

Solution 1

From the documentation

NSLocationWhenInUseUsageDescription (String - iOS) describes the reason why the app accesses the user’s location normally while running in the foreground. Include this key when your app uses location services to track the user’s current location directly. This key does not support using location services to monitor regions or monitor the user’s location using the significant location change service. The system includes the value of this key in the alert panel displayed to the user when requesting permission to use location services.

This key is required when you use the requestWhenInUseAuthorization method of the CLLocationManager class to request authorization for location services. If the key is not present when you call the requestWhenInUseAuthorization method without including this key, the system ignores your request.

This key is supported in iOS 8.0 and later. If your Info.plist file includes both this key and the NSLocationUsageDescription key, the system uses this key and ignores the NSLocationUsageDescription key.

Read about it here.

I find that the easiest way to add this key to your info.plist is to right click you info.plist and choose

Open As->Source Code

and then add the following in the end before </dict></plist>

<key>NSLocationWhenInUseUsageDescription</key>
<string></string>

If you want you can add a text in between <string></string> that describes to the user why you want to use his/hers location. This text will show up under the default text in the alert.

Solution 2

Try writing a NSLocationWhenInUseUsageDescription in Info.plist

Solution 3

iOS 8.3, Xcode 6.3.1, ARC enabled

The question has been resolved, but I have (2) notes to add from my recent involvement with CLLocationManager.

1) You must have the following keys entered into your *.plist file:

enter image description here

Most commonly used keys have generic more descriptive names, such as "Privacy - Location Usage Description", which really is the "NSLocationUsageDescription" key.

To see the "raw" key names go to "*-Info.plist" and right click in the Navigator area where the keys are listed, see below:

enter image description here

And you get the following results:

enter image description here

The three keys that are related to this article are:

enter image description here

2) Make certain that you allocate and initialize your implementation of CLLocationManager before you try to request authorization or update location.

*.h file:

@interface SomeController : UIViewController <CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;

*.m file:

- (IBAction)locationButton:(UIButton *)sender
{
    if (self.locationManager == nil)
    {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
    }
    else
    {
        nil;
    }

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

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];
}

Hope this saves someone time! Thanks.

Solution 4

Here's a little gotcha. Make sure you're adding the NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription keys to the main bundle plist and not one of your test target plists!

Solution 5

Had the same problem caused because I was instantiating CLLocationManager in a local var inside a method, solved it making the CLLocationManager a class property.
After a while I found the solution here, but I'm leaving it here since this is the first result in google, hope I save you some time:

requestWhenInUseAuthorization() not Work in iOS 8 With NSLocationWhenInUseUsageDescription Key in Info.plist

Share:
39,244
Marcus
Author by

Marcus

Updated on January 04, 2020

Comments

  • Marcus
    Marcus over 4 years

    I tried to make my AppProject iOS 8 ready. I had read a lot about

    [_locationManager requestWhenInUseAuthorization];
    

    and the entry in plist

    NSLocationWhenInUseUsageDescription
    

    So I changed all the necessary code lines.

    It works fine, but now I have copied my project again from my iOS 7 base to include new features. But when I make the changes for the iOS8 Location Privacy the Popup doesn't appear anymore.

    My code worked until I copied.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
        <dict>
            <key>NSLocationWhenInUseUsageDescription</key>
            <string>tolle sache </string>
            <key>CFBundleDevelopmentRegion</key>
            <string>en</string>
            <key>CFBundleExecutable</key>
            <string>${EXECUTABLE_NAME}</string>
            <key>CFBundleIdentifier</key>
            <string>fapporite.${PRODUCT_NAME:rfc1034identifier}</string>
            <key>CFBundleInfoDictionaryVersion</key>
            <string>6.0</string>
            <key>CFBundlePackageType</key>
            <string>BNDL</string>
            <key>CFBundleShortVersionString</key>
            <string>1.0</string>
            <key>CFBundleSignature</key>
            <string>????</string>
            <key>CFBundleVersion</key>
            <string>1</string>
        </dict>
    </plist>
    

    and here is my call

    - (instancetype)initWithCoder:(NSCoder *)coder
    {
        self = [super initWithCoder:coder];
        if (self) {
    
            _UserLocation = [[CLLocation alloc]init];
            _locationManager = [[CLLocationManager alloc]init]; // initializing locationManager
            _locationManager.delegate = self;
            _locationManager.desiredAccuracy = kCLLocationAccuracyBest; // setting the accuracy
            [_locationManager requestWhenInUseAuthorization]; // iOS 8 MUST
            [_locationManager startUpdatingLocation];  //requesting location updates
    
            NSLog(@"passed initwithcode");
    
        }
        return self;
    }
    

    How can I fix this?

  • robotspacer
    robotspacer almost 10 years
    That was the main problem for me. After adding the description, the dialog kept popping up and then immediately disappearing. Fixing that required two changes: make sure to request authorization in viewWillAppear: or later, and make sure to store a reference to your CLLocationManager. If you don't it will get deallocated and the dialog disappears.
  • coolcool1994
    coolcool1994 over 9 years
    Perfect Solution for iOS 8!
  • OutOnAWeekend
    OutOnAWeekend over 9 years
    Can even add it in viewDidLoad:, but its important to keep a reference to CLLocationManager.
  • Alejandro Iván
    Alejandro Iván about 9 years
    So (just for the sake of completeness), for keeping a reference, I used a property with a strong modifier: @property (strong, nonatomic) CLLocationManager *locationManager;
  • serge-k
    serge-k over 8 years
    Please leave the blank ...else statements in, that is my programming style, be it right or wrong it helps me keep things organized in my mind. Thanks.
  • sandeep tomar
    sandeep tomar about 8 years
    @Groot sir i still not able to open alert for enable location
  • SimplGy
    SimplGy about 8 years
    Holy cats, @robotspacer. I am so glad you commented. I was doing everything except I had a local reference to locationManager since I'm using a MapView and only needed the location manager to request permissions. I never would have guessed I need to hang on to that reference.
  • Ramakrishna
    Ramakrishna over 7 years
    @Groot, Your answer helped me a lot.