如何在Objective-C中使用startMonitoringForRegion扫描多个区域

我已经经历了两个教程,正在阅读基本的C语言。在过去一周左右的时间里,通过学习做了最好的学习,并写了一些轻量级的应用程序。 赶快写一些将使用ibeacon的应用程序。 正如我正在通过一些样本,阅读参考指南,我看到,可以扫描多个区域通过运行startMonitoringForRegion为每个UUID。 好的,所以我想我可以运行它为每个UUID,但这是行不通的。 我敢肯定,我正在做一些基本上完全错误的东西…下面的代码是一个彻头彻尾的破解 – 一旦我得到的语义,我将从一个数据库调用API的UUIDs,并通过它们来激活监控。 下面的代码在最后一个循环中仅显示四个UUID中的两个。

在标题中:

@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion; @property (strong, nonatomic) CLBeaconRegion *myBeaconRegion2; @property (strong, nonatomic) CLBeaconRegion *myBeaconRegion3; @property (strong, nonatomic) CLBeaconRegion *myBeaconRegion4; 

主要:

 NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"86E4BDEA-C6FF-442C-95CB-E6E557A23CF2"]; self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"com.appcoda.testregion"]; NSUUID *uuid2 = [[NSUUID alloc] initWithUUIDString:@"C9AFF296-A722-4F2D-8669-47B7CCC79A14"]; self.myBeaconRegion2 = [[CLBeaconRegion alloc] initWithProximityUUID:uuid2 identifier:@"com.appcoda.testregion"]; NSUUID *uuid3 = [[NSUUID alloc] initWithUUIDString:@"1DBDDC7C-49BB-48BF-A2F6-A4825BD514EA"]; self.myBeaconRegion3 = [[CLBeaconRegion alloc] initWithProximityUUID:uuid3 identifier:@"com.appcoda.testregion"]; NSUUID *uuid4 = [[NSUUID alloc] initWithUUIDString:@"8D942B9E-0197-4C81-8722-92144599E9F7"]; self.myBeaconRegion4 = [[CLBeaconRegion alloc] initWithProximityUUID:uuid4 identifier:@"com.appcoda.testregion"]; [self.locationManager startMonitoringForRegion:self.myBeaconRegion]; [self.locationManager startMonitoringForRegion:self.myBeaconRegion2]; [self.locationManager startMonitoringForRegion:self.myBeaconRegion3]; [self.locationManager startMonitoringForRegion:self.myBeaconRegion4]; NSSet *setOfRegions = [self.locationManager monitoredRegions]; for (CLRegion *region in setOfRegions) { NSLog (@"region info: %@", region); } 

我认为这个问题是你的地区标识符。 每个信标区域identifier必须是唯一的 ,否则CLLocationManager将它们视为相同的区域。

尝试为每个地区设置一个唯一的标识符:

 NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"86E4BDEA-C6FF-442C-95CB-E6E557A23CF2"]; self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"com.appcoda.testregion"]; NSUUID *uuid2 = [[NSUUID alloc] initWithUUIDString:@"C9AFF296-A722-4F2D-8669-47B7CCC79A14"]; self.myBeaconRegion2 = [[CLBeaconRegion alloc] initWithProximityUUID:uuid2 identifier:@"com.appcoda.testregion2"]; NSUUID *uuid3 = [[NSUUID alloc] initWithUUIDString:@"1DBDDC7C-49BB-48BF-A2F6-A4825BD514EA"]; self.myBeaconRegion3 = [[CLBeaconRegion alloc] initWithProximityUUID:uuid3 identifier:@"com.appcoda.testregion3"]; NSUUID *uuid4 = [[NSUUID alloc] initWithUUIDString:@"8D942B9E-0197-4C81-8722-92144599E9F7"]; self.myBeaconRegion4 = [[CLBeaconRegion alloc] initWithProximityUUID:uuid4 identifier:@"com.appcoda.testregion4"]; 

你应该看到从你的NSLog语句列出的每个区域。 不需要dispatch_async

startMonitoringForRegion的头文件指出“这是asynchronous完成的,可能不会立即反映在monitoredRegions中”。

您可以通过给for循环添加时间延迟来validation这一点:

 double delayInSeconds = 5.0; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ NSSet *setOfRegions = [self.locationManager monitoredRegions]; for (CLRegion *region in setOfRegions) { NSLog (@"region info: %@", region); } }); 

如果您需要使用多个信标进行监控,那么您可以使用信标主要和次要值进行区分。 阅读教程,更好地了解ibeacons。

 -(void)setBeaconTranmitter:(NSInteger)major minorValue:(NSInteger)minor { // We need to set beacon regions here. NSUUID * uid = [[NSUUID alloc] initWithUUIDString:uuid]; //uuid value is static common string for all beacons. self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uid major:major minor:minor identifier:beaconsId];//beaconsId is a common identifier for all beacons. // Call your Transmitter function here [self configureTransmitter]; } 
Interesting Posts