程序被硬关闭后,只能执行这三个回调,而且必须写在appdelegate中,退出ibeacons信息区域时会有延时10秒左右,进入时无延时
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region;
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region;
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region ;
程序被硬关闭后,不能执行回调,之前就是犯错误在这了
- (void)locationManager:(CLLocationManager *)manager
didRangeBeacons:(NSArray *)beacons
inRegion:(CLBeaconRegion *)region;
appdelegate中这样声明即可:
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
然后在页面中启动检测ibeacons信息位置:
NSUUID *proximityUUID = [[NSUUID alloc] initWithUUIDString:kUUID];
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUID identifier:kIdentifier];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.activityType = CLActivityTypeFitness;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//搜索附近ibeacons
[self.locationManager startMonitoringForRegion:self.beaconRegion];
[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
[self.locationManager requestStateForRegion:self.beaconRegion];
然后在plist文件中添加Required background modes选项,增加三个值
App communicates using CoreBluetooth,App shares data using CoreBluetooth,App registers for location updates
然后就可以自己玩了。详尽参考apple的demo-airlocate
附链接https://github.com/linlinqi/AirLocate