macos - Mac CoreLocation 服务不请求权限

标签 macos cocoa core-location

我正在编写一个需要使用 CoreLocation 服务的 Mac 应用程序。只要我在安全首选项 Pane 中手动验证服务,代码和位置就可以正常工作。但是,框架不会自动弹出权限对话框。文档指出:

Important The user has the option of denying an application’s access to the location service data. During its initial uses by an application, the Core Location framework prompts the user to confirm that using the location service is acceptable. If the user denies the request, the CLLocationManager object reports an appropriate error to its delegate during future requests.

我的委托(delegate)确实收到错误,并且 CLLocationManager 上的 +locationServicesEnabled 值是正确的。唯一缺少的部分是向用户提示有关权限的信息。我的开发 MPB 和一个 friend 的 MBP 上都会出现这种情况。我们俩都不知道出了什么问题。

有人遇到过这种情况吗?

相关代码:

_locationManager = [CLLocationManager new];    
[_locationManager setDelegate:self];
[_locationManager setDesiredAccuracy:kCLLocationAccuracyKilometer];
...
[_locationManager startUpdatingLocation];

最佳答案

我发现在 Mac 上,当您通过调用启动位置服务时

[locationManager startUpdatingLocation];

触发

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

状态为

kCLAuthorizationStatusNotDetermined

如果您观察此状态,然后再次开始更新位置,则会触发权限对话框:例如

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
        case kCLAuthorizationStatusAuthorized:
            NSLog(@"Location Services are now Authorised");
            [_locationManager startUpdatingLocation];

            break;

        case kCLAuthorizationStatusDenied:
            NSLog(@"Location Services are now Denied");
            break;

        case kCLAuthorizationStatusNotDetermined:
            NSLog(@"Location Services are now Not Determined");

            //  We need to triger the OS to ask the user for permission.
            [_locationManager startUpdatingLocation];
            [_locationManager stopUpdatingLocation];

            break;

        case kCLAuthorizationStatusRestricted:
            NSLog(@"Location Services are now Restricted");
            break;

        default:
            break;
    }
}

关于macos - Mac CoreLocation 服务不请求权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11254602/

相关文章:

objective-c - 在 NSScrollView 中仅自动隐藏水平滚动条

ios - 不会调用locationManagerShouldDisplayHeadingCalibration

c - 如何在 Mac OS X 上安装 nano-hmac?

objective-c - 在 WebKit 上的 Mac OS 应用程序中启用 WebGL

macos - WKWebView 显示白色条,直到窗口移动/调整大小

objective-c - 更改窗口的委托(delegate)后退出 Mountain Lion 中的全屏中断

objective-c - 如何在 Mac 操作系统上将应用程序设置为默认应用程序?

objective-c - NSInteger 和 decodeInteger :forKey: Problems

ios - 计算准确的 Ibeacon 距离

ios - iBeacon 模式下的 Gimbal S10 未显示在 Apple 的示例 AirLocate 应用程序中