Question

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

Was it helpful?

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.

OTHER TIPS

Yes, you can create a subclass of CLBeacon, but you need to instantiate it manually using a CLBeacon object received from CoreLocation

lets call your subclass MyBeacon, you have to create a init method like

- (id)initWithBeacon:(CLBeacon *)beacon

because CoreLocation will only return CLBeacon objects and not MyBeacon objects, so you need to create your MyBeacon objects by yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top