使用多个ibeacons触发视图

时间:2014-04-22 08:41:19

标签: ios objective-c ipad ibeacon clregion

我正在为iPad开发一个使用多个信标的应用程序,当我进入其区域时我需要显示一个视图,然后,当我靠近一个灯塔并远离另一个时,我需要关闭第一个并打开第二个。

我的代码是:

    -(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    CLBeacon *beacon = [[CLBeacon alloc] init];
    beacon = [beacons lastObject];


    SectionViewController *sectionView = [[SectionViewController alloc] init];
    Sections2ViewController *sectionView2 = [[Sections2ViewController alloc] init];

    if ((beacon.proximity == CLProximityImmediate) && viewShown == NO && beacon.accuracy != -1.000000) {

        if ((beacon.minor.integerValue != actualSection) && actualSection != 0) {
            [self dismissViewControllerAnimated:NO completion:nil];
            actualSection = 0;
            NSLog(@"View Out");
        }

        if (beacon.minor.integerValue  == 1) {
            [self presentViewController:sectionView animated:NO completion:nil];
            sectionView.section = 1;
            actualSection = 1;
            sectionView.lbl_name.text = self.lbl_name.text;
            NSLog(@"View In: 1");
            viewShown = YES;
        }else if (beacon.minor.integerValue == 2){
            [self presentViewController:sectionView2 animated:NO completion:nil];
            sectionView.section = 2;
            actualSection = 2;
            NSLog(@"View In: 2");
            viewShown = YES;
        }
    }else if ((beacon.proximity == CLProximityNear || beacon.proximity == CLProximityFar) && viewShown == YES && beacon.accuracy != -1.000000){
        [self dismissViewControllerAnimated:NO completion:nil];
        viewShown = NO;
    }else{
        NSLog(@"Unknown Distance");
    }

    NSLog(@"\nUUID: %@\nProximity:%ld\nMajor:%@\nMinor:%@\nAccuracy:%f\nRSSI:%ld\nSection: %ld", beacon.proximityUUID.UUIDString, beacon.proximity, beacon.major, beacon.minor, beacon.accuracy, (long)beacon.rssi, (long)self.section);
}
  • sectionView和SectionView2是我想要显示的视图。

  • 我使用viewShown来了解视图是否已呈现或我是否在中 MAINVIEW。

  • actualSection是知道我在哪里。

问题在于,如果我只使用1个信标,它就可以完美运行,但是当我用2做它时,应用程序无法检测到它们并且难以显示正确的视图。

我现在正在尝试,但应用程序只检测到1个信标,并且我在这两个信标上使用相同的UUID并且仅更改次要值。

我现在在iPad Mini视网膜上使用ios 7.0.4,但我很快就会更新到iOS 7.1 - 这是一个问题吗?


解决:

    - (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}

- (void)initRegion {
    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"00000000-0000-0000-0000-000000000002"];
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"com.devfright.myRegion"];
    [self.locationManager startMonitoringForRegion:self.beaconRegion];
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    NSLog(@"Beacon Found");
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSLog(@"Left Region");
    [self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
}
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    CLBeacon *beacon = [[CLBeacon alloc] init];
    beacon = [beacons lastObject];

    //Search for the closest and show the view
    for (int i=0; i<beacons.count; i++) {
        beacon = beacons[i];

        if (beacon.proximity == CLProximityUnknown) {
            NSLog(@"Unknown Proximity");
        } else if ((beacon.proximity == CLProximityImmediate) && viewShown == NO && beacon.accuracy != -1.000000) {
            if ([beacon.minor.stringValue  isEqual: @"1"]) {
                actualSection = 1;
                [self immediateDetection];
            }else if ([beacon.minor.stringValue  isEqual: @"2"]){
                actualSection = 2;
                [self immediateDetection];
            }else if ([beacon.minor.stringValue  isEqual: @"3"]){
                actualSection = 3;
                [self immediateDetection];
            }else if ([beacon.minor.stringValue  isEqual: @"4"]){
                actualSection = 4;
                [self immediateDetection];
            }
        } else if ((beacon.proximity == CLProximityNear || beacon.proximity == CLProximityFar) && viewShown == YES && beacon.accuracy != -1.000000) {
            if (beacon.minor.integerValue == actualSection) {
                [self dismissViewControllerAnimated:NO completion:nil];
                actualSection = 0;
                viewShown = NO;
            }
        } else if (beacon.proximity == CLProximityFar) {
            NSLog(@"Far");
        }
    }
}
- (void)immediateDetection
{
    viewShown = YES;
    if (self.presentedViewController)
    {
        return;
    }
    if (actualSection == 1) {
        SectionViewController *sectionView = [[SectionViewController alloc] init];
        sectionView.section = 1;
        [self presentViewController:sectionView animated:NO completion:nil];
        sectionView.lbl_name.text = self.lbl_name.text;
    }else if (actualSection == 2){
        SectionViewController *sectionView = [[SectionViewController alloc] init];
        sectionView.section = 2;
        [self presentViewController:sectionView animated:NO completion:nil];
        sectionView.lbl_name.text = self.lbl_name.text;
    }
}

现在这是正确的并检测到关闭的一个。感谢

0 个答案:

没有答案