Handling iBeacon Events

To get receive events from iBeacons that are being monitored by the SDK, your app must provide the SDK with a delegate (a class that implements LPBeaconManagerListener). This delegate will be called by the SDK to handle iBeacon events.

@protocol LPBeaconManagerListener <NSObject>

/**
* Method called when one more iBeacons become active
* @param beacons an array of iBeacons
*/
- (void)beaconManager:(id)beaconManager didBecomeActive:(NSArray *)beacons;

/**
* Method called when one more iBeacons become inactive
* @param beacons an array of iBeacons
*/
- (void)beaconManager:(id)beaconManager didBecomeInactive:(NSArray *)beacons;

/**
* Method called when one more iBeacons changes state.
* @param beacons an array of iBeacons
*/
- (void)beaconManager:(id)beaconManager didChange:(NSArray *)beacons;

@end

In example below, the AppDelegate class implements the LPBeaconManagerListener protocol.

//AppDelegate.h
#import <LPSensorLib/LPBeaconManagerListener.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, LPBeaconManagerListener>{
    UIWindow *_window;
}

//AppDelegate.m
#import <UIKit/UIKit.h>
#import <LPSensorLib/LPBeaconManagerListener.h>

@implementation AppDelegate
 
 /**
 * Method called when one more iBeacons become active
 * @param beacons an array of iBeacons
 */
- (void)beaconManager:(id)beaconManager didBecomeActive:(NSArray *)beacons {
    LPBeaconManager *lpBeaconManager = [LPBeaconManager sharedInstance];
    for(LPSensor *sensor in beacons){
      //Fetch iBeacon metadata from the server.
        [lpBeaconManager getSensor:[sensor uuid]
                          andMajor: [[sensor major] intValue]
                          andMinor: [[sensor minor] intValue]
                       didComplete:^(LPSensor *result, BOOL success) {
                                [self processSensorMetadata:result];
                       }];
    }
    
}
//Call different actions depending on the metadata on the iBeacon.
- (void)processSensorMetadata:(LPSensor *)sensor {
    NSDictionary *metadata = [sensor metadata];
    if([metadata objectForKey:@"checkin_action"] != nil){
        [self handleCheckinAction:sensor withActionDictionary:[metadata objectForKey:@"checkin_action"]];
    }else if([[sensor metadata] objectForKey:@"show_asset_action"] != nil){
        [self handleShowAssetAction:[metadata objectForKey:@"show_asset_action"]];
    }else if([[sensor metadata] objectForKey:@"inbox_add_assets_action"] != nil){
        [self handleInboxAddAssetsAction:[metadata objectForKey:@"inbox_add_assets_action"]];
    }else if([[sensor metadata] objectForKey:@"inbox_save_assets_action"] != nil){
        [self handleInboxSaveAssetsAction:[metadata objectForKey:@"inbox_save_assets_action"]];
    }
}

/**
* Method called when one more iBeacons become inactive
* @param beacons an array of iBeacons
*/
- (void)beaconManager:(id)beaconManager didBecomeInactive:(NSArray *)beacons {

}

/**
* Method called when one more iBeacons changes state.
* @param beacons an array of iBeacons
*/
- (void)beaconManager:(id)beaconManager didChange:(NSArray *)beacons {
  //
}