- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试为 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 中已弃用)将包括 kCLAuthorizationStatusAuthorizedAlways
和 kCLAuthorizationStatusAuthorizedWhenInUse
因为它们都是特殊类型的“授权” .
感谢@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/
我是一名优秀的程序员,十分优秀!