requestAlwaysAuthorization未显示权限警报

Car*_*tor 27 core-location ios ibeacon

我试图使用一些花哨的iBeacons没有成功,kCLAuthorizationStatusNotDetermined一直.根据其他问题,需要将这些密钥添加到info.plist(一些问题说一个,另一个问题说两者都有).根据iBeacons的一篇文章,我需要Always选项.

<key>NSLocationWhenInUseUsageDescription</key>
<string>Nothing to say</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Permiso para acceder siempre</string>
Run Code Online (Sandbox Code Playgroud)

在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];
Run Code Online (Sandbox Code Playgroud)

在执行最后两种方法之一之前,图标未出现在"设置/隐私/位置"中.永远不会出现批准权限的警报视图.如果我在"位置设置"中执行手动更改并进行检查,则会更改状态,但过了一会儿,"设置"中的位置将删除我的应用的"始终"状态,并将其再次留空.后来我检查没有运气

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
Run Code Online (Sandbox Code Playgroud)

什么缺失或错误的想法?谢谢

Eli*_*ist 34

对于iOS 11开发人员,您应该看一下这篇文章:位置服务在iOS 11中不起作用.


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

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>...</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>...</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>...</string>
Run Code Online (Sandbox Code Playgroud)

随着InfoPlist.strings多语言应用程序的翻译.


小智 19

有完全相同的问题.事实证明,在我的情况下NSLocationAlwaysUsageDescription,我的InfoPlist.strings本地化文件也是必需的.有NSLocationAlwaysUsageDescriptionInfo.plist是不够的......


bal*_*oth 13

我注意到,如果在警报显示之前销毁了CLLocationManager实例,您将永远不会看到警报.在我的例子中,我在AppDelegate中创建了一个位置管理器的局部变量来请求权限.

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    [locationManager requestAlwaysAuthorization];
}
Run Code Online (Sandbox Code Playgroud)

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

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

_locationManager = [[CLLocationManager alloc] init];
if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    [_locationManager requestAlwaysAuthorization];
}
Run Code Online (Sandbox Code Playgroud)


小智 10

我最近在尝试使用 IOS 11 时遇到了类似的问题

locationManager.requestAlwaysAuthorization()
Run Code Online (Sandbox Code Playgroud)

我的解决方案是在 plist.info 中添加所有 4 个权限以获取警报:

  • 隐私 - 使用时的位置 使用说明
  • 隐私 - 位置使用说明
  • 隐私 - 位置始终使用说明
  • 隐私 - 位置始终和使用时使用说明


Art*_*tov 7

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

<key>NSLocationAlwaysUsageDescription</key>
<string>Optional message</string>
Run Code Online (Sandbox Code Playgroud)


小智 3

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

[self.locationManager startUpdatingLocation];
Run Code Online (Sandbox Code Playgroud)