gpt4 book ai didi

macos - Mac CoreLocation 服务不请求权限

转载 作者:行者123 更新时间:2023-12-03 16:06:36 25 4
gpt4 key购买 nike

我正在编写一个需要使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com