Search code examples
ioscllocationmanagercllocationibeacon

subclass ClBeacon to store some extra informations


it's possible to subclass of CLBeacon to store some extra informations and handle those informations in the delegate method

- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region

where beacons is array of MyCustomBeaconSubclass


Solution

  • Yes, this is possible. Various frameworks do exactly this. The key is that you also have to write wrapper classes around the CoreLocation functions so that when you get a didRangeBeaconsInRegion callback, you get instances of your subclass instead of the CLBeacon class.

    Building all these wrapper classes can be complex, and it is often easier to use an off-the-shelf framework that does this. My company offers one called ProximityKit that allows you to attach extra information to iBeacons as key/value pairs. You assign key/value pairs to iBeacons in the cloud using a web interface. Then you use the ProximityKit classes to range your iBeacons, which allows you to access the extra information exactly as you describe.

    Here's an example retrieves the value of a field called "messageForUser" from a ranged iBeacon. Note that this uses a subclass of CLBeacon called PKIBeacon:

    - (void)proximityKit:(PKManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(PKRegion *)region {
        [beacons enumerateObjectsUsingBlock:^(id beaconObj, NSUInteger beaconIdx, BOOL *beaconStop) {        
            PKIBeacon *beacon = (PKIBeacon *) beaconObj;        
            // The value of messageForUser is set in the ProximityKit web interface
            NSString *messageForUser = [beacon.attributes objectForKey:@"messageForUser"]; 
            NSLog(@"The value of messageForUser for iBeacon %@ %ld %ld is: %@", beacon.uuid, beacon.major, beacon.minor, messageForUser);
        }];
    }
    

    Full disclosure: I am Chief Engineer at Radius Networks.