Created
September 9, 2015 22:07
-
-
Save caseycoding/0602617b222263d807d2 to your computer and use it in GitHub Desktop.
iBeacon Ranger View Controller with two regions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// iBeaconRanger | |
// | |
// | |
// | |
#import "ViewController.h" | |
#import <CoreLocation/Corelocation.h> | |
@interface ViewController () <CLLocationManagerDelegate> | |
@property (nonatomic) CLLocationManager *locationManager; | |
@property (nonatomic) CLBeaconRegion *region1; | |
@property (nonatomic) CLBeaconRegion *region2; | |
@end | |
@implementation ViewController | |
- (CLBeaconRegion *)region1 | |
{ | |
if (!_region1) { | |
_region1 = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString: @"61687109-905F-4436-91F8-E602F514C96D" ] major: 3 minor: 22464 identifier:@"com.beacons.region1"]; | |
_region1.notifyOnEntry = YES; | |
_region1.notifyOnExit = YES; | |
_region1.notifyEntryStateOnDisplay = YES; | |
} | |
return _region1; | |
} | |
- (CLBeaconRegion *)region2 | |
{ | |
if (!_region2) { | |
_region2 = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString: @"61687109-905F-4436-91F8-E602F514C96D" ] major: 3 minor: 22463 identifier:@"com.beacons.region2"]; | |
_region2.notifyOnEntry = YES; | |
_region2.notifyOnExit = YES; | |
_region2.notifyEntryStateOnDisplay = YES; | |
} | |
return _region2; | |
} | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
self.locationManager = [[CLLocationManager alloc] init]; | |
self.locationManager.delegate = self; | |
if (CLLocationManager.authorizationStatus == kCLAuthorizationStatusNotDetermined) { | |
[self.locationManager requestAlwaysAuthorization]; | |
} | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
} | |
#pragma mark - Private methods | |
- (NSString *)stringForRegionState:(CLRegionState)state | |
{ | |
NSString *string = nil; | |
switch (state) { | |
case CLRegionStateInside: | |
string = @"Inside"; | |
break; | |
case CLRegionStateOutside: | |
string = @"Outside"; | |
break; | |
case CLRegionStateUnknown: | |
default: | |
string = @"Unknown"; | |
break; | |
} | |
return string; | |
} | |
- (NSString *)stringForAuthorizationStatus:(CLAuthorizationStatus)status | |
{ | |
NSString *string = nil; | |
switch (status) { | |
case kCLAuthorizationStatusAuthorizedAlways: | |
string = @"Always"; | |
break; | |
case kCLAuthorizationStatusAuthorizedWhenInUse: | |
string = @"WhenInUse"; | |
break; | |
case kCLAuthorizationStatusRestricted: | |
string = @"Restricted"; | |
break; | |
case kCLAuthorizationStatusNotDetermined: | |
string = @"NotDetermined"; | |
break; | |
case kCLAuthorizationStatusDenied: | |
string = @"Denied"; | |
break; | |
default: | |
string = @"Unknown"; | |
break; | |
} | |
return string; | |
} | |
#pragma mark - CLLocationManagerDelegate methods | |
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status | |
{ | |
NSLog(@"Did change authorization status %@", [self stringForAuthorizationStatus:status]); | |
if (CLLocationManager.authorizationStatus == kCLAuthorizationStatusAuthorizedAlways) { | |
for (CLRegion *region in self.locationManager.monitoredRegions) { | |
if ([region isKindOfClass:[CLBeaconRegion class]]) { | |
[self.locationManager stopRangingBeaconsInRegion:(CLBeaconRegion *)region]; | |
} | |
[self.locationManager stopMonitoringForRegion:region]; | |
} | |
[self.locationManager startMonitoringForRegion:self.region1]; | |
[self.locationManager startRangingBeaconsInRegion:self.region1]; | |
[self.locationManager requestStateForRegion:self.region1]; | |
[self.locationManager startMonitoringForRegion:self.region2]; | |
[self.locationManager startRangingBeaconsInRegion:self.region2]; | |
[self.locationManager requestStateForRegion:self.region2]; | |
} | |
} | |
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error | |
{ | |
NSLog(@"Monitoring did fail for region %@ with error %@", region.identifier, [error localizedDescription]); | |
} | |
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region | |
{ | |
NSLog(@"Did enter region with identifier %@", region.identifier); | |
[self.locationManager startRangingBeaconsInRegion:(CLBeaconRegion *)region]; | |
} | |
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region | |
{ | |
NSLog(@"Did exit region with identifier %@", region.identifier); | |
[self.locationManager stopRangingBeaconsInRegion:(CLBeaconRegion *)region]; | |
} | |
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region | |
{ | |
NSLog(@"Did start monitoring for region with identifier %@", region.identifier); | |
} | |
- (void) locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region | |
{ | |
NSLog(@"Determined state %@ for region with identifier %@", [self stringForRegionState:state], region.identifier); | |
switch (state) { | |
case CLRegionStateInside: | |
[self.locationManager startRangingBeaconsInRegion:(CLBeaconRegion *)region]; | |
break; | |
case CLRegionStateOutside: | |
[self.locationManager stopRangingBeaconsInRegion:(CLBeaconRegion *)region]; | |
break; | |
case CLRegionStateUnknown: | |
default: | |
break; | |
} | |
} | |
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region | |
{ | |
NSLog(@"Did range %lu beacons in region with identifier %@", (unsigned long)beacons.count, region.identifier); | |
for (CLBeacon *beacon in beacons) { | |
NSLog(@"%@:%ld:%ld rssi: %ld dBm", beacon.proximityUUID.UUIDString, (long)[beacon.major integerValue], (long)[beacon.minor integerValue], (long)beacon.rssi); | |
} | |
} | |
- (void)locationManager:(CLLocationManager *)manager rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region withError:(NSError *)error | |
{ | |
NSLog(@"Ranging beacons did fail for region %@ with error %@", region.identifier, [error localizedDescription]); | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment