Search code examples
iosobjective-cxcodeibeaconviewdidload

Custom Init in viewDidLoad for iBeacons


I am working with the ESTimote SDK for iBeacons and they have you call a custom init method in their samples. However I am working with a storyboard and the story board doesn't call my custom init method. So customer support suggested I move the custom init method to inside the viewDidLoad but I cannot seem to figure it out. When using the custom init method the current region is null. If i hard code the UUID,major and minor it works great.

   -(id)initWithBeacon:(ESTBeacon *)beacon
{
    self = [super init];
    if (self)
    {
        self.beacon = beacon;
        NSLog(@"Initiation got called");
    }
    return self;
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"B9407f30-f5f8-466e-aff9-25556b57fe6d"];

    self.beacon = [[ESTBeacon alloc] init];
    self.beaconManager = [[ESTBeaconManager alloc] init];
    self.beaconManager.delegate = self;
    self.beacon = _beacon;
    self.beaconRegion = [[ESTBeaconRegion alloc] initWithProximityUUID:uuid
                                                                 major:37470
                                                                 minor:56023
                                                            identifier:@"RegionIdentifier"];

    self.secondBeaconRegion = [[ESTBeaconRegion alloc] initWithProximityUUID:self.beacon.proximityUUID
                                                                 major:[self.beacon.major unsignedIntValue]
                                                                 minor:[self.beacon.minor unsignedIntValue]
                                                            identifier:@"RangingRegion"];

    if (self.beaconRegion) {
        NSLog(@"youve got your region");
    }

    if (self.secondBeaconRegion) {
        NSLog(@"the second beacon region");
    }else{
        NSLog(@"the second region doesnt get intialized ");
    }

    self.beaconRegion.notifyOnEntry = YES;
    self.beaconRegion.notifyOnExit = YES;

    [self.beaconManager startMonitoringForRegion:self.beaconRegion];
    [self.beaconManager startRangingBeaconsInRegion:self.secondBeaconRegion];
    [self.beaconManager requestAlwaysAuthorization];
}

Solution

  • If you are using Storyboards then a different constructor is being called. XIBs and Storyboards are using initWithCoder:(NSCoder*)aDecoder constructor.

    If you want to pass beacon to your controller then you need to pass them during segue. See Apple's docs.