iOS下iBeacon总结

权限

要使用iBeacon监控,要先做以下检查

  • 检查设备是否支持Beacon扫描
if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]) {
}
  • 必须获取Always定位权限
if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways) {
    [CLLocationManager requestAlwaysAuthorization];
}
  • 且必须打开后台获取地理位置更新权限

Important
Apps must have always authorization to use region monitoring, and they must be configured with the Location updates background mode to be launched.

Beacon属性

  • proximityUUID (The unique ID of the beacons being targeted.)
  • major (The value identifying a group of beacons.)
  • minor (The value identifying a specific beacon within a group.)

实现

一般先检测Beacon的进出(Enter/Exit),再获取具体距离(Ranging),且苹果建议只在Foreground情况下Ranging

CLBeaconRegion* region = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:uuidStr];
region.notifyEntryStateOnDisplay = YES;
[locationManager startMonitoringForRegion:region];

// CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    [manager startRangingBeaconsInRegion:(CLBeaconRegion *)region];
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    [manager stopRangingBeaconsInRegion:(CLBeaconRegion *)region];
}

- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
}

关于后台运行

当App不在前台或者被杀死是,也可以感应iBeacon信号,系统会唤醒App并给予一小段时间(10秒左右)进行处理。
这时可以使用 [UIApplication beginBackgroundTaskWithExpirationHandler:] 请求更多后台执行时间

1个App最多能同时监控20个regions

Core Location limits to 20 the number of regions that may be simultaneously monitored by a single app.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容