如何在Objective-C中使用startMonitoringForRegion扫描多个区域 [英] How to scan multiple regions using startMonitoringForRegion in Objective-C

查看:92
本文介绍了如何在Objective-C中使用startMonitoringForRegion扫描多个区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过两个教程,并且正在阅读有关C的基本知识.通过学习,可以学到最好的东西,并且在过去一周左右的时间内编写了一些简单的应用程序.正在加快编写一些将使用ibeacon的应用程序的速度.在浏览一些示例并阅读参考指南时,我发现可以通过为每个UUID运行startMonitoringForRegion来扫描多个区域.好的,所以我想我可以为每个UUID运行它,但这是行不通的.我确定我在做一些基本完全错误的事情...下面的代码是完全破解-一旦获得语义,我将使用API​​调用从数据库中提取UUID,然后遍历它们以激活监视.下面的代码导致最后一个循环仅显示四个UUID中的两个.

I've been through two tutorials and am reading up on basic C. Learn best through doing and have written a few light apps in the past week or so. Am getting up to speed to write some apps that will use ibeacon. As I'm going through some samples and reading the reference guide I see that multiple regions can be scanned by running startMonitoringForRegion for each UUID. OK, so I figure I could just run it for each UUID but that's not working. I'm sure I'm doing something basic totally wrong... the code below is a total hack - once I get the semantics I will pull the UUIDs from a DB with an API call and than loop through them to activate the monitoring. The code below results in last loop only showing two of the four UUIDs.

标题中:

@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将它们视为相同区域.

I think the problem is your region identifiers. Each beacon region identifier must be unique, otherwise CLLocationManager treats them as the same region.

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

Try setting a unique identifier for each region:

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.

You should see each region listed from your NSLog statement. No need for dispatch_async, either.

这篇关于如何在Objective-C中使用startMonitoringForRegion扫描多个区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆