requestAlwaysAuthorization不显示权限警报

我试图用一些花哨的iBeacons没有成功,kCLAuthorizationStatusNotDetermined所有的时间。 根据其他问题,这是一个要求将这些键添加到info.plist(一些问题说一个,其他人都说)。 根据一篇关于iBeacons的文章,我需要Always选项。

<key>NSLocationWhenInUseUsageDescription</key> <string>Nothing to say</string> <key>NSLocationAlwaysUsageDescription</key> <string>Permiso para acceder siempre</string> 

在viewDidAppear:

 self.locManager = [[CLLocationManager alloc]init]; self.locManager.delegate = self; [self.locManager requestAlwaysAuthorization]; NSUUID* region1UUID = [[NSUUID alloc]initWithUUIDString:@""]; //ibeacon real UUID between "". Checked it's not nil. self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUID identifier:@"myCoolString"]; self.beaconRegion.notifyEntryStateOnDisplay = YES; self.beaconRegion.notifyOnEntry = YES; self.beaconRegion.notifyOnExit = NO; [self.locManager startMonitoringForRegion:self.beaconRegion]; [self.locManager startRangingBeaconsInRegion:self.beaconRegion]; 

设置/隐私/位置图标没有出现在最后两个方法中的一个之前。 警报视图批准权限从不出现。 如果我在位置设置中执行手动更改并检查它会改变状态,但是稍后在设置中的位置将删除我的应用程序的“始终”状态,并将其保留为空白。 后来我检查没有运气

 -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { 

任何想法是什么缺失或错误? 谢谢

有完全一样的问题。 事实certificate,在我的情况下, NSLocationAlwaysUsageDescription在我的InfoPlist.strings本地化文件中也是必需的。 在Info.plist中使用NSLocationAlwaysUsageDescription是不够的…

我注意到,如果您的CLLocationManager实例在警报显示之前被销毁,您将永远不会看到警报。 在我的情况下,我在AppDelegate中创build位置pipe理器的本地variables来请求权限。

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

将局部variables更改为实例variables使警报显示:

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

只需将这些行添加到.plist文件即可

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

对于iOS 11开发者,你应该看看这篇文章: 定位服务不适用于iOS 11 。


TL; DR:您需要Info.plist中的所有三个位置键:

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

随着InfoPlist.strings翻译多种语言的应用程序的情况下。

发现,在论坛上logging和testing它是Objective-C iOS 8相关的错误。 相同的代码写在Swift只是起作用。 工作swift代码,委托是调用。

将Core Location框架添加到项目设置/目标/function/背景模式设置“位置更新”和“使用Bluetooth LE附件”在Info.plist上添encryption钥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() } 

对话框显示OK

确保将密钥添加到正确的Info.plist文件中。 不要忘记有一个为你的应用程序和一个为AppTest。

Swift 3.X最新的代码很容易使用

 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)") } 

尝试开始更新位置(帮助我)

 [self.locationManager startUpdatingLocation]; 

请检查这个参考链接

它描述了有关iOS 11的所有变化。您的问题似乎也是它描述的情况之一。 它可能会让您知道需要在代码中进行哪些更改才能解决问题。

我复制了这个教程…

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

它没有解决scheme,虽然修复非常简单,不要声明

让locationManager = CLLocationManager()

但将其作为variables移入类中

var locationManager = CLLocationManager()

它的工作原理!

它会显示一个提示,当您的应用程序的位置权限未设置,并且一旦您的应用程序的“使用”位置权限设置,后续调用不显示(我不认为有任何API反馈电话被吞下)。

从Apple文档 :

讨论当前授权状态为“未确定”时,此方法asynchronous运行,并提示用户授予应用程序使用位置服务的权限。 用户提示包含应用程序Info.plist文件中NSLocationAlwaysUsageDescription项的文本,调用此方法时需要该项的存在。 状态确定后,位置pipe理器将结果传递给委托的locationManager( :didChangeAuthorization :)方法。 如果当前授权状态是notDetermined以外的任何其他方法,则此方法不执行任何操作,也不调用locationManager( :didChangeAuthorization :)方法,但有一个例外。 如果您的应用程序从未请求授权并且当前的授权状态为授权状态,则您可以调用此方法一次尝试将应用程序的授权状态更改为authorizedAlways。