requestAlwaysAuthorization not showing permission alert

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 08:10:41

For iOS 11 developers, you should take a look at this post: Location Services not working in iOS 11.


TL;DR: you need ALL THREE location keys in the Info.plist:

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>...</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>...</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>...</string>

Along with InfoPlist.strings translations in case of multilingual apps.

chris

Had exactly the same problem. It turned out that in my case the NSLocationAlwaysUsageDescription was required in my InfoPlist.strings localization files as well. Having NSLocationAlwaysUsageDescription in Info.plist wasn't enough...

I have noticed that if your instance of CLLocationManager is destroyed before the alert shows, you will never see the alert. In my case I was creating a local variable of the location manager in the AppDelegate to ask for permission.

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    [locationManager requestAlwaysAuthorization];
}

Changing the local variable to an instance variable made the alert to display:

@interface AppDelegate () {
    CLLocationManager *_locationManager;
}
@end

_locationManager = [[CLLocationManager alloc] init];
if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    [_locationManager requestAlwaysAuthorization];
}

Just add this lines to your .plist file

<key>NSLocationAlwaysUsageDescription</key>
<string>Optional message</string>

I got a similar problem recently with IOS 11 when I tried to use

locationManager.requestAlwaysAuthorization()

The solution for me was to add all the 4 permissions in plist.info to get the alert:

  • Privacy - Location When In Use Usage Description
  • Privacy - Location Usage Description
  • Privacy - Location Always Usage Description
  • Privacy - Location Always and When In Use Usage Description

Try to Start Updating Location (have helped for me)

[self.locationManager startUpdatingLocation];

Found, documented at forums and tested it's an Objective-C iOS 8 related bug. Same code written in Swift just works. Working swift code, delegate is call.

Add Core Location framework to Project Settings / Targets / Capabilities / Background Modes set "Location Updates" and "Uses Bluetooth LE Accessories" Add key at Info.plist NSLocationAlwaysUsageDescription

import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate  {

var locManager: CLLocationManager?

override func viewDidLoad() {
    super.viewDidLoad()
    self.locManager = CLLocationManager();
    self.locManager!.delegate = self;
    if (!CLLocationManager.locationServicesEnabled()) {
        println("Location services are not enabled");
    }
    self.locManager!.requestAlwaysAuthorization();
    self.locManager!.pausesLocationUpdatesAutomatically = false;
    let uuidString = ""  // My ibeacon string there
    let beaconIdentifier = "myCompany"
    let beaconUUID:NSUUID = NSUUID(UUIDString: uuidString)
    let beaconRegion:CLBeaconRegion = CLBeaconRegion(proximityUUID: beaconUUID,
        identifier: beaconIdentifier)
    self.locManager!.startMonitoringForRegion(beaconRegion)
    self.locManager!.startRangingBeaconsInRegion(beaconRegion)
    self.locManager!.startUpdatingLocation()
}

Dialog appears OK

Make sure you add the keys to the correct Info.plist file. Don't forget there's one for your App and one for AppTest.

Swift 3.X Latest code easily usage

import CoreLocation

 public var locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()


        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()

}



    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        let altitudeG = locations.last?.altitude
        let longitudeG = locations.last?.coordinate.longitude
        let latitudeG = locations.last?.coordinate.latitude

print("\(altitudeG) \(longitudeG) \(latitudeG)")

    }

I copied this tutorial ...

http://willd.me/posts/getting-started-with-ibeacon-a-swift-tutorial

It didn't work out the box, although the fix was very simple, don't declare

let locationManager = CLLocationManager()

But move it into the class as variable

var locationManager = CLLocationManager()

And it works!!

It will show a prompt when the location permission for your app isn't set and once when 'in use' location permission for your app is set, subsequent calls do not show (and I don't think there is any API feedback that the call was swallowed).

From Apple docs:

Discussion When the current authorization status is notDetermined , this method runs asynchronously and prompts the user to grant permission to the app to use location services. The user prompt contains the text from the NSLocationAlwaysUsageDescription key in your app’s Info.plist file, and the presence of that key is required when calling this method. After the status is determined, the location manager delivers the results to the delegate’s locationManager(:didChangeAuthorization:) method. If the current authorization status is anything other than notDetermined, this method does nothing and does not call the locationManager(:didChangeAuthorization:) method, with one exception. If your app has never requested always authorization and its current authorization status is authorizedWhenInUse , you may call this method one time to try and change your app's authorization status to authorizedAlways .

Please check this Reference link

It has described all the changes regarding iOS 11. Your issue is also seems to be one among the cases it described. It might give you an idea what changes you need to make in your code to correct the issue.

For ios 11 Its important to add below mention new permission which is

NSLocationAlwaysAndWhenInUseUsageDescription

along with the old permission which is

NSLocationAlwaysUsageDescription, NSLocationWhenInUseUsageDescription

to fix the requestAlwaysAuthorization not showing permission alert.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!