第一.ibeacon的扫描实现
1.导入头文件,调用代理和定义属性
#import <CoreLocation/CoreLocation.h>
<CLLocationManagerDelegate>
@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion;
@property (strong, nonatomic) CLLocationManager *locationManager;
2.配置info.plist相关参数
4BFBDBDF-699D-4FEF-B008-D05AE9FA1C60.png
3.初始化相关变量
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
//传入自己使用的ibeacon的UUID
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"e2c56db5-dffb-48d2-b060-d0f5a71096e0"];
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:@"com.appcoda.testregion"];
[self.myBeaconRegion peripheralDataWithMeasuredPower:@-30];
[_locationManager requestAlwaysAuthorization];
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
4.调用相关方法
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region
{
NSLog(@"开始。。。");
// [self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
}
-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion*)region
{
NSLog(@"结束。。。");
// [self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];
}
-(void)locationManager:(CLLocationManager*)manager
didRangeBeacons:(NSArray*)beacons
inRegion:(CLBeaconRegion*)region
{
// CLBeacon *foundBeacon = [beacons firstObject];
NSLog(@"-----------beacons: %@",beacons);
// NSLog(@"----------region: %@",region);
// NSLog(@"---------%@",manager);
}
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
NSLog(@"Failed monitoring region: %@", error);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"Location manager failed: %@", error);
}
第二.相关知识链接和demo实现
网友评论