ios - CLLocationManager 权限

标签 ios objective-c cllocationmanager

我正在处理位置权限,接下来是问题: 用户从隐私中关闭了位置服务并安装了该应用程序。我有一行代码要求启用定位服务: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/

相关文章:

html - 如何在不选择 IOS 画廊的情况下强制从相机上传文件?

ios - 如何在 Xamarin.Forms 中设置 ListView 行高

插入换行符后 iOS 8 UITextView 滚动到插入符号问题

ios - 带约束的移动 View

ios - 我怎样才能使新的 UILabel 的默认颜色为黑色(而不是透明的)

ios - 未知异常: signal SIGPIPE with crash when device is unlocked after locked screen

ios - 如何在 PageViewController 中将 CGGradients 混合在一起?

ios - 在 Swift 的 MKMapView 中显示当前位置和更新位置

ios - 为什么在我允许后没有使用定位服务?

objective-c - GPS 应用程序在 iOS 5 上停止工作 - 位置管理器未更新