ios requestAlwaysAuthorization 未显示权限警报
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26005122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
requestAlwaysAuthorization not showing permission alert
提问by Carlos Pastor
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 info.plist (some questions says one, other says both). According to an article for iBeacons I need the Always option.
我一直在尝试使用一些花哨的 iBeacons,但没有成功,kCLAuthorizationStatusNotDetermined。根据其他问题,需要将这些键添加到 info.plist (有些问题说一个,其他说两个)。根据 iBeacons 的一篇文章,我需要 Always 选项。
<key>NSLocationWhenInUseUsageDescription</key>
<string>Nothing to say</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Permiso para acceder siempre</string>
At viewDidAppear:
在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];
Icon didn't appear at Settings/Privacy/Location until it was executed one of the two last methods. The Alert View to approve permissions never appears. If I perform a manual change at Location Settings and check it it will change status but at a few moments later Location at Settings will delete "Always" status for my app and will leave it blank again. Later I check with no luck
图标没有出现在设置/隐私/位置,直到它被执行最后两个方法之一。永远不会出现批准权限的警报视图。如果我在“位置设置”中执行手动更改并检查它会更改状态,但稍后“设置中的位置”将删除我的应用程序的“始终”状态,并将其再次留空。后来我检查没有运气
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
Any ideas what is missing or wrong? Thank you
任何想法丢失或错误?谢谢
回答by Elist
For iOS 11 developers, you should take a look at this post: Location Services not working in iOS 11.
对于 iOS 11 开发人员,您应该看看这篇文章:定位服务在 iOS 11 中不起作用。
TL;DR: you need ALL THREE location keys in the Info.plist:
TL;DR:您需要 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.
随着InfoPlist.strings
多语种应用程序的情况下翻译。
回答by 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...
有完全相同的问题。事实证明,在我的情况下NSLocationAlwaysUsageDescription
,我的InfoPlist.strings
本地化文件也需要。有NSLocationAlwaysUsageDescription
在Info.plist
是不够的......
回答by balkoth
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 实例在警报显示之前被销毁,您将永远不会看到警报。就我而言,我在 AppDelegate 中创建了位置管理器的局部变量以请求许可。
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];
}
回答by Artyom Devyatov
Just add this lines to your .plist file
只需将此行添加到您的 .plist 文件中
<key>NSLocationAlwaysUsageDescription</key>
<string>Optional message</string>
回答by Bachir
I got a similar problem recently with IOS 11 when I tried to use
我最近在尝试使用 IOS 11 时遇到了类似的问题
locationManager.requestAlwaysAuthorization()
The solution for me was to add all the 4 permissions in plist.info to get the alert:
我的解决方案是在 plist.info 中添加所有 4 个权限以获取警报:
- 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
- 隐私 - 使用时的位置 使用说明
- 隐私 - 位置使用说明
- 隐私 - 位置始终使用说明
- 隐私 - 位置始终和使用时使用说明
回答by Anthony Marchenko
Try to Start Updating Location (have helped for me)
尝试开始更新位置(对我有帮助)
[self.locationManager startUpdatingLocation];
回答by Carlos Pastor
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.
发现,在论坛上记录并测试它是一个与 Objective-C iOS 8 相关的错误。用 Swift 编写的相同代码也能正常工作。工作快速代码,委托是调用。
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
将核心位置框架添加到项目设置/目标/功能/后台模式设置“位置更新”和“使用蓝牙 LE 附件”在 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
对话框出现 OK
回答by Josh B
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.
确保将密钥添加到正确的 Info.plist 文件中。不要忘记有一个用于您的应用程序,一个用于 AppTest。
回答by SwiftDeveloper
Swift 3.X Latestcode easily usage
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)")
}
回答by user3159598
For ios 11 Its important to add below mention new permission which is
对于 ios 11 重要的是添加下面提到的新权限
NSLocationAlwaysAndWhenInUseUsageDescription
NSLocationAlwaysAndWhenInUseUsageDescription
along with the old permission which is
连同旧的许可
NSLocationAlwaysUsageDescription, NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription,NSLocationWhenInUseUsageDescription
to fix the requestAlwaysAuthorization not showing permission alert.
修复 requestAlwaysAuthorization 未显示权限警报的问题。