0

我在玩 Corelocation 和 iBeacons。我在进入和退出一个区域时触发了通知,我可以对我的信标进行范围等。这一切都很好。

但是我陷入了混乱。我想在靠近特定信标时加载第二个视图,然后在我们离开该信标时关闭该视图,冲洗并重复第二个信标。

我正在努力:

  • 由于测距没有停止,如何停止触发视图变化。如果我手动停止测距或使用 bool 来测试我是否已经在第二个视图中,这没关系,但看起来很乱。

  • 如果我离开信标,如何关闭视图。要做到这一点,我想我不能停止测距,否则我不知道我是否搬走了。

我的代码如下。

    - (void)viewDidLoad
{
    [super viewDidLoad];

}

- (void)viewDidAppear:(BOOL)animated {

    // Setup Beacon Manager
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:@"B9407F30-F5F8-466E-AFF9-25556B57FE6D"];
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"Beacon Region"];
    [self.locationManager startMonitoringForRegion:self.beaconRegion];
    [self.locationManager requestStateForRegion:self.beaconRegion];
    self.beaconStatLabel.text = @"StartLocationServices";



    //check to see if this is the first time we've run the app
    if ([[NSUserDefaults standardUserDefaults] floatForKey:@"tBHasRun"] == 0) {
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"tBHasRun"];  //set the time run to 1
        [self performSegueWithIdentifier:@"firstRunSegue" sender:self];
        self.beaconStatLabel.text = @"FIRST RUN"; //set the label text
    } else {
        self.beaconStatLabel.text = @"REPEAT RUN"; //set the label text
    }

}


    //Looking for and dealing with regions

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
    //looking for a region means looking for a beacon or set of beacons that share a UUID
    [self.locationManager requestStateForRegion:self.beaconRegion];
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    //if we found a region we start ranging (looking for beaocns)
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
    self.regionState.text = @"Region Entered";

    //we'll also test sending a notification
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody = @"Welcome! Go upstairs, bring beer.";
    notification.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    //we have left the region so we'll stop ranging
    [self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
    self.regionState.text = @"Region Exited";

    //we'll also test sending a notification
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody = @"Thankyou for coming.  For information on our next MeetUp check our MeetUp page.";
    notification.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

    //dealing with individual beacons

- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    //once beacons are ranged we enter this method

    //we'll grab and log the signal strength of the beacons
    for (int i = 0; i < [beacons count]; i++) {
        CLBeacon *singleBeacon = [[CLBeacon alloc]init];
        singleBeacon = [beacons objectAtIndex:i];
    }


    //we get the latest beacon in the array - the closest beacon (strongest signal)
    CLBeacon *beacon = [[CLBeacon alloc] init];
    beacon = [beacons lastObject];

    //update the info labels
    self.uuidLabel.text = beacon.proximityUUID.UUIDString;
    self.majorLabel.text = [NSString stringWithFormat:@"%@", beacon.major];
    self.minorLabel.text = [NSString stringWithFormat:@"%@", beacon.minor];

    //we store some information about that beacon
    NSNumber *beaconMajor = beacon.major;  //it's major (group) number
    NSNumber *beaconMinor = beacon.minor;  //it's minor (individual) number

    //we then call the manageBeacon method and pass through the minor, major, and proximity values
    [self manageBeaconWithMinor:beaconMinor AndMajor:beaconMajor AtRange:beacon.proximity];

}

- (void)manageBeaconWithMinor:(NSNumber *)minorNumber AndMajor:(NSNumber *)majorNumber AtRange:(CLProximity)proximity {
    //in this method we work out what do do based upon the beacon we are connected to and the range

    //for this test we'll look for the mint beacon and call a view
    if (([minorNumber floatValue] == 59204) && ([majorNumber floatValue] == 33995) && (proximity == CLProximityNear)) {
        //we are going to open up content
        [[tBGlobalStore sharedInstance]setInContentTrue];
        NSLog([[tBGlobalStore sharedInstance] getInContent] ? @"Yes" : @"No");
        //the beacon numbers match the beacon we are expecting so we'll call the next screen
        [self performSegueWithIdentifier:@"mainToContent" sender:self];
    }
}

来自控制台的一些附加信息,您可以看到它试图多次调用视图。

2013-11-22 15:24:56.487 测试信标[670:60b] --- 2013-11-22 15:24:56.489 测试信标[670:60b] ----- 2013-11-22 15:24:56.490 testingBeacons[670:60b] 信标位于:2013-11-22 15:24:56.490 testingBeacons[670:60b] 0 2013-11-22 15:24:56.491 testingBeacons[670:60b] 的 RSSI 为:2013-11- 22 15:24:56.492 testingBeacons[670:60b] -75 2013-11-22 15:24:56.492 testingBeacons[670:60b] ----- 2013-11-22 15:24:56.493 testingBeacons[670:60b ] --- 2013-11-22 15:24:56.495 testingBeacons[670:60b] 警告:尝试显示不在窗口层次结构中的视图!2013-11-22 15:24:56.497 测试信标[670:60b] --- 2013-11-22 15:24:56.498 测试信标[670:60b] ----- 2013-11-22 15:24:56.498 testingBeacons[670:60b] 信标位于:2013-11-22 15:24:56.499 testingBeacons[670:60b] 0 2013-11-22 15:24:56.499 testingBeacons[670:60b] 的 RSSI 为:2013-11- 22 时 15 分 24 分 56 秒。500 个测试信标[670:60b] -75 2013-11-22 15:24:56.500 测试信标[670:60b] ----- 2013-11-22 15:24:56.501 测试信标[670:60b] --- 2013 -11-22 15:24:57.487 testingBeacons[670:60b] --- 2013-11-22 15:24:57.489 testingBeacons[670:60b] ----- 2013-11-22 15:24:57.489 testingBeacon [670:60b] 信标位于:2013-11-22 15:24:57.490 testingBeacons[670:60b] 0 2013-11-22 15:24:57.490 testingBeacons[670:60b] 的 RSSI 为:2013-11-22 15:24:57.491 测试信标[670:60b] -75 2013-11-22 15:24:57.491 测试信标[670:60b] ----- 2013-11-22 15:24:57.492 测试信标[670:60b] --- 2013-11-22 15:24:57.493 testingBeacons[670:60b] 警告:尝试显示不在窗口层次结构中的视图!2013-11-22 15:24:57.495 测试信标[670:60b] --- 2013-11-22 15:24:57.495 测试信标[670:60b] ----- 2013-11-22 15:24:57.496 testingBeacons[670:60b] 信标在:2013-11-22 15:24:57.496 testingBeacons[670:60b] 0 2013-11-22 15:24:57.497 testingBeacons[670:60b] 的 RSSI 为:2013-11-22 15:24:57.497 testingBeacons[670 :60b] -75 2013-11-22 15:24:57.498 测试信标[670:60b] ----- 2013-11-22 15:24:57.499 测试信标[670:60b] --- 2013-11-22 15:24:57.500 testingBeacons[670:60b] 警告:尝试呈现不在窗口层次结构中的视图!2013-11-22 15:24:58.488 测试信标[670:60b] --- 2013-11-22 15:24:58.489 测试信标[670:60b] ----- 2013-11-22 15:24:58.490 testingBeacons[670:60b] 信标位于:2013-11-22 15:24:58.490 testingBeacons[670:60b] 0 2013-11-22 15:24:58.491 testingBeacons[670:60b] 的 RSSI 为:2013-11- 22 15:24:58.491 测试信标[670:60b] -76 2013-11-22 15:24:58.492 测试信标[670:60b] ----- 2013-11-22 15:24:58.492 测试信标[670:60b ] --- 2013-11-22 15:24:58。493 testingBeacons[670:60b] 警告:尝试显示不在窗口层次结构中的视图!2013-11-22 15:24:58.494 测试信标[670:60b] --- 2013-11-22 15:24:58.495 测试信标[670:60b] ----- 2013-11-22 15:24:58.496 testingBeacons[670:60b] 信标位于:2013-11-22 15:24:58.496 testingBeacons[670:60b] 0 2013-11-22 15:24:58.497 testingBeacons[670:60b] 的 RSSI 为:2013-11- 22 15:24:58.497 testingBeacons[670:60b] -76 2013-11-22 15:24:58.498 testingBeacons[670:60b] ----- 2013-11-22 15:24:58.499 testingBeacons[670:60b ] --- 2013-11-22 15:24:58.500 testingBeacons[670:60b] 警告:尝试显示不在窗口层次结构中的视图!2013-11-22 15:24:59.488 测试信标[670:60b] --- 2013-11-22 15:24:59.489 测试信标[670:60b] ----- 2013-11-22 15:24:59.489 testingBeacons[670:60b] 信标于:2013-11-22 15:24:59。490 testingBeacons[670:60b] 0 2013-11-22 15:24:59.490 testingBeacons[670:60b] 的 RSSI 为:2013-11-22 15:24:59.491 testingBeacons[670:60b] -75 2013-11- 22 15:24:59.491 testingBeacons[670:60b] ----- 2013-11-22 15:24:59.492 testingBeacons[670:60b] --- 2013-11-22 15:24:59.493 testingBeacons[670: 60b] 警告:尝试呈现不在窗口层次结构中的视图!2013-11-22 15:24:59.494 测试信标[670:60b] --- 2013-11-22 15:24:59.495 测试信标[670:60b] ----- 2013-11-22 15:24:59.495 testingBeacons[670:60b] 信标位于:2013-11-22 15:24:59.496 testingBeacons[670:60b] 0 2013-11-22 15:24:59.496 testingBeacons[670:60b] 的 RSSI 为:2013-11- 22 15:24:59.497 测试信标[670:60b] -75 2013-11-22 15:24:59.498 测试信标[670:60b] ----- 2013-11-22 15:24:59.498 测试信标[670:60b ] --- 2013-11-22 15:24:59.500 测试信标[670: 60b] 警告:尝试呈现不在窗口层次结构中的视图!2013-11-22 15:25:00.487 测试信标[670:60b] --- 2013-11-22 15:25:00.488 测试信标[670:60b] ----- 2013-11-22 15:25:00.489 testingBeacons[670:60b] 信标位于:2013-11-22 15:25:00.489 testingBeacons[670:60b] 0 2013-11-22 15:25:00.490 testingBeacons[670:60b] 的 RSSI 为:2013-11- 22 15:25:00.490 测试信标[670:60b] -75 2013-11-22 15:25:00.491 测试信标[670:60b] ----- 2013-11-22 15:25:00.491 测试信标[670:60b ] --- 2013-11-22 15:25:00.492 testingBeacons[670:60b] 警告:尝试显示不在窗口层次结构中的视图!2013-11-22 15:25:00.493 测试信标[670:60b] --- 2013-11-22 15:25:00.494 测试信标[670:60b] ----- 2013-11-22 15:25:00.494 testingBeacons[670:60b] 信标位于:2013-11-22 15:25:00.495 testingBeacons[670: 60b] 0 2013-11-22 15:25:00.495 testingBeacons[670:60b] 的 RSSI 为:2013-11-22 15:25:00.496 testingBeacons[670:60b] -75 2013-11-22 15:25: 00.496 testingBeacons[670:60b] ----- 2013-11-22 15:25:00.497 testingBeacons[670:60b] --- 2013-11-22 15:25:00.498 testingBeacons[670:60b] 警告:尝试呈现不在窗口层次结构中的视图!2013-11-22 15:25:01.488 测试信标[670:60b] --- 2013-11-22 15:25:01.489 测试信标[670:60b] ----- 2013-11-22 15:25:01.489 testingBeacons[670:60b] 信标位于:2013-11-22 15:25:01.490 testingBeacons[670:60b] 0 2013-11-22 15:25:01.490 testingBeacons[670:60b] 的 RSSI 为:2013-11- 22 15:25:01.491 testingBeacons[670:60b] -72 2013-11-22 15:25:01.492 testingBeacons[670:60b] ----- 2013-11-22 15:25:01.492 testingBeacons[670:60b ] --- 2013-11-22 15:25:01.493 testingBeacons[670:60b] 警告:尝试呈现不在窗口层次结构中的视图!2013-11-22 15:25:01.494 测试信标[670:60b] --- 2013-11-22 15:25:01.495 测试信标[670:60b] ----- 2013-11-22 15:25:01.495 testingBeacons[670:60b] 信标位于:2013-11-22 15:25:01.496 testingBeacons[670:60b] 0 2013-11-22 15:25:01.497 testingBeacons[670:60b] 的 RSSI 为:2013-11- 22 15:25:01.497 testingBeacons[670:60b] -72 2013-11-22 15:25:01.498 testingBeacons[670:60b] ----- 2013-11-22 15:25:01.498 testingBeacons[670:60b ] --- 2013-11-22 15:25:01.499 testingBeacons[670:60b] 警告:尝试呈现不在窗口层次结构中的视图!

4

2 回答 2

4

我们遇到了类似的挑战。但是这两种解决方案都有一个问题:Apple 处理测距和接近度的方式存在一个“错误”,你会得到随机切换和 didExitRegion 调用。

看这里:

http://beekn.net/2013/11/ibeacon-tutorial-dealing-with-errors/

您的应用会看似随机调用 didExitRegion,这显然是一个错误,因为位置服务实际上可以完全关闭一秒钟左右。

我们处理了 viewController 的想法,方法是在 viewController 被推送后停止测距,然后在用户关闭 viewController 后重新启动它。

  1. 之后:[self performSegueWithIdentifier:@"mainToContent" sender:self];

添加:

[self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];

  1. 为了在用户关闭 viewController 后重新开始测距,添加:

// 当 OutsideViewController 被推入时 ViewWillAppear 重启 stopRanging - (void)viewWillAppear:(BOOL)animated { [self.locationManager startRangingBeaconsInRegion:self.beaconRegion]; }

注意:如果你想根据区域变化自动关闭 viewController,你会遇到快速推拉的问题:你会推送一个 viewController,然后它会快速关闭、打开、关闭......问题不是代码,而是 Apple API 无法始终确定它在哪个区域。

可能的解决方案?“黑客”可能是添加一个计数器:我们发现,您将手机靠近信标,即使您没有移动手机并且信标信号也没有移动,它也会在接近/立即之间快速切换'实际上并没有改变。这是一个苹果的“错误”!但它只会这样做一两次。

一种选择是添加一个计数器:相当于“仅当您检测到区域更改三到四次时才触发事件”

另一个选项是添加时间:“仅在区域更改 4 秒后才触发基于区域更改的事件”

再一次 - 问题不在于你会突然切换(推动 VC 或将其从堆栈中拉出),因为你在“区域之间” - 问题是:

A. Apple API 无法始终正确判断您是直接/近/远,并且会突然改变主意

B. Apple 有时会看似随机关闭定位服务,持续时间不到一秒,迫使您的应用重新进入某个区域并重新开始测距。

于 2013-11-23T12:01:41.647 回答
1

didRangeBeacons:每个信标每秒调用一次,并且不会为您跟踪附近的变化。

CLLocationManagerDelegate 参考

我建议做的是将您的信标跟踪封装在它自己的类中,该类负责:

  1. 管理信标监控
  2. 保留特定 iBeacon 及其当前范围的哈希值。
  3. 当特定 iBeacon 的范围发生变化时调用协议方法通知委托(您的视图控制器)甚至 NSNotificationCenter(您知道这是因为您正在维护最近的信标/接近度的哈希)。
于 2013-11-22T15:27:14.203 回答