唤起app

iOS下iBeacon总结

2017-06-13  本文已影响0人  Jun_Chen

权限

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

if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]) {
}
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属性

实现

一般先检测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.

上一篇 下一篇

猜你喜欢

热点阅读