requestAlwaysAuthorization not showing permission alert

前端 未结 17 1065
我寻月下人不归
我寻月下人不归 2020-12-29 20:19

I\'m trying to use some fancy iBeacons without success, kCLAuthorizationStatusNotDetermined all time. According to other questions it\'s a requirement to add those keys to

相关标签:
17条回答
  • 2020-12-29 21:04

    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 .

    0 讨论(0)
  • 2020-12-29 21:04

    For me on iOS 13, I had to reset the Simulator to trigger the permission prompt again.

    0 讨论(0)
  • 2020-12-29 21:06

    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.

    0 讨论(0)
  • 2020-12-29 21:07

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

    0 讨论(0)
  • 2020-12-29 21:09

    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.

    0 讨论(0)
  • 2020-12-29 21:12

    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

    0 讨论(0)
提交回复
热议问题