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

 拍友2502869537 发布于 2023-02-02 18:57

我已经完成了两个教程并且正在阅读基本的C.通过做最好的学习并在过去一周左右编写了一些轻量级的应用程序.我正在加快编写一些将使用ibeacon的应用程序.当我浏览一些示例并阅读参考指南时,我看到可以通过为每个UUID运行startMonitoringForRegion来扫描多个区域.好的,所以我想我可以为每个UUID运行它,但那不起作用.我确定我正在做一些基本上完全错误的事情......下面的代码是一个彻底的黑客攻击 - 一旦我得到语义,我将从带有API调用的数据库中提取UUID,然后循环遍历它们以激活监视.下面的代码导致最后一个循环只显示四个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);
    }

James Frost.. 11

我认为问题是您的区域标识符.每个信标区域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.

1 个回答
  • 我认为问题是您的区域标识符.每个信标区域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.

    2023-02-02 18:58 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有