IOS - ibeacon

2017-04-28  本文已影响73人  LeoDavid

<b>CLLocationManager</b>

//需要初始化位置服务

//初始化
self.locationmanager = [[CLLocationManager alloc] init];

//实现代理
self.locationmanager.delegate = self;

//设置location是一直允许
[self.locationmanager requestAlwaysAuthorization];

//监听范围内的ibeacon
[self.locationmanager startMonitoringForRegion:self.beacon];

//接受到ibeacon的信息通知
[self.locationmanager startRangingBeaconsInRegion:self.beacon];

</b>
<b>CLBeaconRegion</b>

//通过UUID标识符初始化一个ibeacon
//ibeacon处了用UUID识别,还可以有major,minor识别。major表示ibeacon的group的value,minor表示每个ibeacon在group中的value
self.beacon = [[CLBeaconRegion alloc] 
initWithProximityUUID:[[NSUUID alloc]
initWithUUIDString:BEACONUUID]identifier:[[[NSUUID alloc] initWithUUIDString:BEACONUUID] UUIDString]];//初始化监测的iBeacon信息

</b>
<b>CLLocationManagerDelegate</b>

//获取ibeacon信息,范围内的ibeacon,当设置了startRangingBeaconsInRegion,可以接收到这个代理
- (void)locationManager:(CLLocationManager *)manager rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region withError:(NSError *)error; //error
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region; //success

//发现有iBeacon进入监测范围,当设置了startMonitoringForRegion可以接收到,
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region 
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region


- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
//位置授权回掉,当requestAlwaysAuthorization 授权成功时回掉

</b>
<b>ibeacon后台通知</b>

//是否接受ibeacon的通知,默认NO。
self.beacon.notifyEntryStateOnDisplay = YES;

//当位YES时,CLLocationManagerDelegate会首先调用这个代理
- (void)locationManager:(CLLocationManager *)manager
      didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
上一篇下一篇

猜你喜欢

热点阅读