ibeacon
是苹果公司在ios7
发布的一款硬件,可以感知ibeacon
的位置。
ibeacon
只是一个硬件设备,具体我们不过多的聊了,因为我是做软件,更关心代码怎么写。
使用ibeacon
的一些代理方法,必须要开启定位权限。
- 开启定位权限,在plist文件中加入 plist -> source code 添加一下代码
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>允许</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>允许</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>允许</string>
在appdelegate
中代码清单如下:
CLAuthorizationStatus state = [CLLocationManager authorizationStatus];
switch (state) {
case kCLAuthorizationStatusNotDetermined:
{
// 用户没有做出选择
if ([self.locationManger respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManger requestAlwaysAuthorization];
}
}
break;
case kCLAuthorizationStatusRestricted:
{
// 没有获得用户授权
}
break;
case kCLAuthorizationStatusDenied:
{
// 用户明确禁止了
}
break;
case kCLAuthorizationStatusAuthorizedAlways:
{
// 用户选择总是运行访问位置
}
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
{
// 用户选择运行期间访问位置
}
break;
default:
break;
}
位置权限获取了。我们开始创建一个region 代码清单如下
- (CLBeaconRegion *)beaconRegion
{
if (!_beaconRegion) {
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"00000000-1111-2222-3333-444444444444"];
_beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"region"];
// 用户离开ibeacon区域,会走退出的代理方法
// 感觉 YES 和NO 都不会影响----
_beaconRegion.notifyOnExit = YES;
_beaconRegion.notifyOnEntry = YES;
// 设置成Yes,每次屏幕点亮,都会调用locationManager:didDetermineState:forRegion:
_beaconRegion.notifyEntryStateOnDisplay = YES;
}
return _beaconRegion;
}
创建region 有一下几个方法
initWithProximityUUID:identifier:
initWithProximityUUID:major:identifier:
initWithProximityUUID:major:minor:identifier:
uuid
、major
、minor
都是ibeacon
的一些属性。。硬件工程师都会告诉你的。identifier
就是region
的标志,目的是区分不同的region
两种监听方式:
startMonitoringForRegion:能监听用户的离开和进入,前台后台都能
startMonitoringForRegion :能监听用户距离ibeacon的距离等一些信息,不能在后台监听
代理方法
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
// 离开
NSLog(@"离开");
LocalPush *localNotification = [[LocalPush alloc] init];
localNotification.title = @"测试";
localNotification.body = @"离开了";
localNotification.soundName = nil;
localNotification.delayTimeInterval = 0.0;
[localNotification pushLocalNotification];
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
// 进入
NSLog(@"进入");
LocalPush *localNotification = [[LocalPush alloc] init];
localNotification.title = @"测试";
localNotification.body = @"进入了";
localNotification.soundName = nil;
localNotification.delayTimeInterval = 0.0;
[localNotification pushLocalNotification];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"失败");
}
- (void)locationManager:(CLLocationManager *)manager
monitoringDidFailForRegion:(nullable CLRegion *)region
withError:(NSError *)error
{
NSLog(@"monitoringDidFail");
}
// 在后台和APP被kill .. 设置了notifyEntryStateOnDisplay = yes ,每次点亮屏幕都会被调用
- (void)locationManager:(CLLocationManager *)manager
didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
if(state == CLRegionStateInside) {
NSLog(@"locationManager didDetermineState INSIDE for %@", region.identifier);
}
else if(state == CLRegionStateOutside) {
NSLog(@"locationManager didDetermineState OUTSIDE for %@", region.identifier);
}
else {
NSLog(@"locationManager didDetermineState OTHER for %@", region.identifier);
}
}
// 没一秒执行一次
- (void)locationManager:(CLLocationManager *)manager
didRangeBeacons:(NSArray<CLBeacon *> *)beacons inRegion:(CLBeaconRegion *)region
{
for (CLBeacon *beacon in beacons) {
NSLog(@" rssi is :%ld",(long)beacon.rssi);
NSLog(@" beacon proximity :%ld",(long)beacon.proximity);
NSLog(@" accuracy : %f",beacon.accuracy);
NSLog(@" proximityUUID : %@",beacon.proximityUUID.UUIDString);
NSLog(@" major :%ld",(long)beacon.major.integerValue);
NSLog(@" minor :%ld",(long)beacon.minor.integerValue);
}
}
总结注意点:
1、必须要开启蓝牙服务 (理解错误了,不需要开启蓝牙)
2、必须要开启定位服务,如果想后台和被kill的状态下,必须是完全允许定位,而不是使用期间允许定位
3、只能监听20个region
密码: j5rp
网友评论