我正在处理位置权限,接下来是问题:
用户从隐私中关闭了位置服务并安装了该应用程序。我有一行代码要求启用定位服务:if (![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)...
这工作得很好。问题是应用程序不要求允许应用程序使用位置,然后第二次询问。请求权限代码:
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
注意:如果启用位置服务,一切正常,它会第一次请求批准。
编辑:权限的完整代码:
-(void)setupPermissions
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if (![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
}
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
}
最佳答案
您的代码毫无意义。你有这个:
if (something) {
}
if (somethingelse) {
[self.locationManager requestWhenInUseAuthorization];
}
所以你有一个 if
是空的,另一个 if
总是 运行,不管第一个 if
确实如此。也许您打算对第二个条件使用 else if
?
(请记住,正如您已经被告知的那样,调用 requestWhenInUseAuthorization
是没有意义的,除非状态为 NotDetermined。)
关于ios - CLLocationManager 权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38021727/