更新:这不是重复的。我已经按照我原来的问题中所述将所需的密钥添加到 info.plist 中,但问题仍然存在。我已经尝试过所有三个键的各种组合。
在任何人感到不安之前,我已经阅读了许多 Apple Dev 论坛帖子和堆栈溢出帖子,并且无法弄清楚为什么我的应用程序拒绝提示用户允许“使用时”授权。
我已将以下键添加到我的
Info.plist
文件中,并附带一个字符串值:
NSLocationWhenInUseUsageDescription
然后我编写了(用 Swift 和 Obj-C)应该提示用户的代码:
@property CLLocationManager *location;
...
@synthesize location;
...
location = [[CLLocationManager alloc] init];
location.delegate = self;
location.desiredAccuracy = kCLLocationAccuracyBest;
location.distanceFilter = kCLDistanceFilterNone;
[location requestWhenInUseAuthorization];
[location startUpdatingLocation];
I'm using the following CLLocationManagerDelegate methods.
- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
- (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
这基本上是直接从Apple“LocateMe”示例代码复制的。
无论我尝试什么不同的顺序或微小的改变,应用程序都不会提示允许授权。我使用 switch 语句来确定
[CLLocationManager authorizationStatus]
的状态,但不断收到“未确定”响应。
if ([CLLocationManager locationServicesEnabled]) {
switch ([CLLocationManager authorizationStatus]) {
case kCLAuthorizationStatusAuthorizedAlways:
NSLog(@"Always Authorized");
break;
case kCLAuthorizationStatusDenied:
NSLog(@"Denied");
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"Authorized in Use");
break;
case kCLAuthorizationStatusNotDetermined:
NSLog(@"Not Determined");
break;
case kCLAuthorizationStatusRestricted:
NSLog(@"Restricted");
break;
}
}
任何帮助将不胜感激。我正在使用 iOS 8.1.2 物理设备和 iOS 8.2 (12D5452a) 模拟器运行 Xcode 6.2 (6C101) 进行测试。
如果您更改了 iPhone 模拟器中的位置设置,这可能会导致提示不显示。例如,如果您已经通过允许位置访问 iOS 提示设置了位置权限,并做出了决定,该决定将存储在 iPhone 模拟器的隐私设置中,因此后续调用
locationManager.requestAlwaysAuthorization()
或 locationManager.requestWhenInUseAuthorization()
将不会显示 iOS 提示因为您已经为您的应用程序定义了隐私 -> 位置设置。
要清除 iPhone 模拟器的设置:
"iOS Simulator" -> "Reset Content and Settings..."
我还在 Xcode 中进行了 Clean 构建,并在某个时候退出并重新启动了 iPhone 模拟器,我可能错误地认为这部分有任何帮助......
我遇到了完全相同的问题,经过两天的研究,结果发现该应用程序没有从项目导航器窗格中的捆绑包中包含的 info.plist 文件读取,因为它已本地化为另一种语言并且正在使用基本国际化,这意味着 Finder 中将有 3 个 info.plist 文件:
1- 在“测试”文件夹下
2-在base.lproj文件夹下
3- ar.lprj 下(ar 指阿拉伯语)。
该项目从 base.lproj 的版本读取,该版本未包含在项目导航器窗格中的捆绑包内。
我所做的是,我备份了我想要的 plist 文件(上面的数字 2),并删除了项目导航器内的 info.plist 文件的本地化,并选择从磁盘删除以将其完全删除,然后我获取备份副本并将其放置在 Finder 中的项目根目录下,然后将其导入回 Xcode,现在项目从新的 info.plist 文件中读取,并且密钥 NSLocationWhenInUseUsageDescription 会在应用程序启动时触发用户授权警报。
希望这有帮助。
这里有很多有用的比较:http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
另外,愚蠢的提问时间:您混合了 _location 和 self.location 但没有显示您的属性/合成代码。您确定您正在操作同一个对象吗?
我发现在您的 info.plst 文件中您不能使用 Privacy - Location Use Description 键,您必须有一个名为
NSLocationWhenInUseUsageDescription
的键和一个值。通过将此密钥添加到我的 info.plist 中,我能够使您的代码正常工作
#import <MapKit/MapKit.h>
@interface ViewController ()
@property CLLocationManager * location;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_location = [[CLLocationManager alloc] init];
self.location.delegate = self;
self.location.desiredAccuracy = kCLLocationAccuracyBest;
self.location.distanceFilter = kCLDistanceFilterNone;
[_location requestWhenInUseAuthorization];
[self.location startUpdatingLocation];
}
这可能很愚蠢,但我只是花了近 2 个小时来反复检查我能找到的有关 CLLocationManager 权限的所有答案。
当我终于意识到我有 2 个 info.plist 文件并且我使用了错误的文件时,我感到很愚蠢!
您可以检查项目的构建设置 - 有一个名为 Info.plist 文件的值。
只是想把它留在这里供未来的读者使用。
我浪费了 1-2 天的时间。 仅从 info.plist 更新在我的情况下不起作用。
相反,如果您导航到
TARGETS -> YOUR_APP_NAME -> Info section
您可以看到自定义iOS目标属性,在这里您可以看到目的字符串。更新您的位置、联系人、通知等目的字符串。确保权限在 info.plist 中也可见。