gpt4 book ai didi

ios - 带有 "When In Use"位置访问错误代码 4 的 CLLocationManager requestStateForRegion

转载 作者:可可西里 更新时间:2023-11-01 05:33:07 30 4
gpt4 key购买 nike

我正在尝试为 iOS 应用实现 iBeacon 测距。

[locationManager requestAlwaysAuthorization];
CLBeaconRegion * region = [self regionFromUUID:uuid];
[locationManager startMonitoringForRegion:region];

为了判断设备是在区域内还是区域外:

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
[locationManager requestStateForRegion:region];
}

这成功调用了:

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
if (state == CLRegionStateInside) {
[locationManager startRangingBeaconsInRegion:(CLBeaconRegion*)region];
} else {
[locationManager stopRangingBeaconsInRegion:(CLBeaconRegion*)region];
}
}

并且该应用程序已成功使用 locationManager:didRangeBeacons:inRegion:

我遇到的问题是使用requestWhenInUseAuthorization。在 locationManager:didStartMonitoringForRegion: 调用 [location requestStateForRegion:region] 之后,委托(delegate)方法 locationManager:monitoringDidFailForRegion:withError: 返回错误代码 4:“The操作无法完成”。

startRangingBeaconsInRegion 交换 requestStateForRegion 似乎可以绕过这个错误,并且 locationManager:didRangeBeacons:inRegion: 被成功调用。

这是一个已知问题吗,如果仅授予 kCLAuthorizationStatusAuthorizedWhenInUse[locationManager requestStateForRegion:region]; 将导致错误代码 4?

最佳答案

Region Monitoring 的 Apple 文档这段摘录让我很困扰:

If the authorization status is kCLAuthorizationStatusAuthorized, your app can receive boundary crossing notifications for any regions it registered. If the authorization status is set to any other value, the app doesn’t receive those notifications.

我在想 kCLAuthorizationStatusAuthorized(在 iOS 8 中已弃用)将包括 kCLAuthorizationStatusAuthorizedAlwayskCLAuthorizationStatusAuthorizedWhenInUse 因为它们都是特殊类型的“授权” .

感谢@heypiotr,我决定实际查看 Apple Docs Declaration并注意到枚举声明如下:

kCLAuthorizationStatusAuthorized,
kCLAuthorizationStatusAuthorizedAlways = kCLAuthorizationStatusAuthorized,
kCLAuthorizationStatusAuthorizedWhenInUse

因此,requestStateForRegion 需要 kCLAuthorizationStatusAuthorizedAlways,因为这是唯一与 kCLAuthorizationStatusAuthorized 相同的值,并且根据 Apple,只有 kCLAuthorizationStatusAuthorized 将与监控一起工作。

关于ios - 带有 "When In Use"位置访问错误代码 4 的 CLLocationManager requestStateForRegion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36072792/

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