Beacon didRangeBeacons in Region Not Callled

时间:2014-12-16 12:49:33

标签: objective-c xcode ibeacon estimote

几个月前我的代码工作得很好。我知道任何事情

我不明白为什么当我启动应用程序时,Region中的didRangeBeacons不会触发:

#import "ESTViewController.h"
#import "PresentViewController.h"
#import <ESTBeaconManager.h>
#import <Parse/Parse.h>
#import <AudioToolbox/AudioToolbox.h>

@interface ESTViewController () <ESTBeaconManagerDelegate>

@property (nonatomic, strong) ESTBeaconManager* beaconManager;

@property (nonatomic, strong) ESTBeacon* selectedBeacon;

@property (nonatomic, strong) NSArray *beaconsArray;

@end

@implementation ESTViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    // setup Estimote beacon manager.

    // Create the manager instance.
    self.beaconManager = [[ESTBeaconManager alloc] init];
    // Setup the delegate
    self.beaconManager.delegate = self;
    // Avoiding unknown state or not
    self.beaconManager.avoidUnknownStateBeacons = NO;
    // create sample region object (beacons in this case have all same uuid and same major)
    region = [[ESTBeaconRegion alloc] initWithProximityUUID:ESTIMOTE_PROXIMITY_UUID identifier:@"multibeacons"];


    [self.beaconManager requestStateForRegion:region];

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // start looking for estimote beacons in region
    // when beacon ranged beaconManager:didRangeBeacons:inRegion: invoked
    [self.beaconManager startRangingBeaconsInRegion:region];
    [self.beaconManager startMonitoringForRegion:region];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    // stop looking for estimote beacons in region
    [self.beaconManager stopRangingBeaconsInRegion:region];
    [self.beaconManager stopMonitoringForRegion:region];

}

#pragma mark - ESTBeaconManager delegate

    -(void)beaconManager:(ESTBeaconManager *)manager
     didRangeBeacons:(NSArray *)beacons
            inRegion:(ESTBeaconRegion *)region
{
    // descriptor on distance to sort the array of beacons by distance
    NSSortDescriptor *sortDescriptor;
    sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

    // sorting the array of beacons
    // beacon array is sorted based on distance
    // closest beacon is the first one
    self.beaconsArray = [beacons sortedArrayUsingDescriptors:sortDescriptors];

    if([self.beaconsArray count] > 0)
    {

        if(!self.selectedBeacon)
        {
            // initialy pick closest beacon
            self.selectedBeacon = [beacons objectAtIndex:0];
            currentBeaconMinor = self.selectedBeacon.minor;
        }
        else
        {

            //sorting the array of beacons
           self.beaconsArray = [beacons sortedArrayUsingDescriptors:sortDescriptors];

           //updating the selected beacon with the first element of the array (closest beacon)
           if(self.selectedBeacon != [beacons objectAtIndex:0] )
            {
                self.selectedBeacon = [beacons objectAtIndex:0];
                currentBeaconMinor = self.selectedBeacon.minor;
            }

        }

        // Switch on proximity of the closest beacon
        switch (self.selectedBeacon.proximity)
        {
            case CLProximityUnknown:
            {
                self.rangeStatusImageView.image = [UIImage imageNamed:@"lost.jpg"];
                self.rangeStatusImageView.hidden = NO;
                self.signalStatusImageView.hidden = YES;
                self.descriptionStateLabel.text = @"Recherche de signal ... Si le problème persiste, contactez l'accueil";

                [UIView animateWithDuration:1.0

                                      delay: 0.0

                                    options: UIViewAnimationOptionCurveEaseIn

                                 animations:^{

                                     self.rangeStatusImageView.alpha = 0.3;

                                 }

                                 completion:^(BOOL finished){


                                     [UIView animateWithDuration:1.0

                                                           delay: 0.0

                                                         options:UIViewAnimationOptionCurveEaseOut

                                                      animations:^{

                                                          self.rangeStatusImageView.alpha = 1.0;

                                                      }

                                                      completion:nil];

                                 }];

                break;
            }
            case CLProximityImmediate:
            {

                if ([currentBeaconMinor floatValue] == 128)
                {
                    NSLog(@"128 128 128");
                    [self performSegueWithIdentifier: @"presentSegue1" sender: self];
                }

                else if ([currentBeaconMinor floatValue] == 228)
                {
                    NSLog(@"228 228 228");
                    [self performSegueWithIdentifier: @"presentSegue2" sender: self];
                }
                else if ([currentBeaconMinor floatValue] == 328)
                {
                    NSLog(@"328 328 328");
                    [self performSegueWithIdentifier: @"presentSegue3" sender: self];
                }

                break;
            }
            case CLProximityNear:
            {
                self.rangeStatusImageView.image = [UIImage imageNamed:@"near.jpg"];
                self.signalStatusImageView.hidden = NO;

                AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

                [UIView animateWithDuration:1.0

                                      delay: 0.0

                                    options: UIViewAnimationOptionCurveEaseIn

                                 animations:^{

                                     self.rangeStatusImageView.alpha = 0.3;

                                 }

                                 completion:^(BOOL finished){


                                     [UIView animateWithDuration:1.0

                                                           delay: 0.0

                                                         options:UIViewAnimationOptionCurveEaseOut

                                                      animations:^{

                                                          self.rangeStatusImageView.alpha = 1.0;

                                                      }

                                                      completion:nil];

                                 }];


                self.rangeStatusImageView.hidden = NO;
                self.descriptionStateLabel.font = [UIFont systemFontOfSize:17];
                self.descriptionStateLabel.text = @"Approchez vous d'un article pour obtenir plus d'informations";
                break;

            }
            case CLProximityFar:
            {
                self.rangeStatusImageView.image = [UIImage imageNamed:@"far.jpg"];
                [self.rangeStatusImageView stopAnimating];
                self.rangeStatusImageView.hidden = NO;
                self.signalStatusImageView.hidden = NO;
                self.descriptionStateLabel.text = @"Bienvenue dans notre ... ";
                break;
            }

            default:
                break;

        }
        self.beaconsArray = [beacons sortedArrayUsingDescriptors:sortDescriptors];
    }
}

我在pragma标记后面有一个断点,它永远不会达到该代码。我真的不明白为什么。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

我找到了解决方案:

我用以下内容更新了Info.plist文件:

<key>NSLocationAlwaysUsageDescription</key>
<string>This application monitors your location to show you promotional offers in shops you're passing by.</string>

在iphone中,我已将我的应用的本地化设置更改为&#34;始终&#34;