Skip to content

Commit

Permalink
complete CLLocationManagerDelegate implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
James Heller committed Jun 23, 2014
1 parent 3cb00b8 commit d36f1a7
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Example/SJHLocationManager/SJHLocationHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ - (instancetype)init {
if (self) {
SJHLocationManager *locationManager = [SJHLocationManager sharedInstance];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didUpdateLocation:) name:kLocationManagerDidUpdateLocation object:locationManager];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didUpdateLocation:) name:kLocationManagerDidUpdateLocations object:locationManager];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFailLocationUpdateWithError:) name:kLocationManagerDidFailWithError object:locationManager];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEnterRegion:) name:kLocationManagerDidEnterRegion object:locationManager];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didExitRegion:) name:kLocationManagerDidExitRegion object:locationManager];
Expand Down
23 changes: 21 additions & 2 deletions SJHLocationManager/SJHLocationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,39 @@

/***************************************************************************************
*
*
*
* /////////////////////////////////////////////////////////////////////////////////////
* Notifications are posted to [NSNotificationCenter defaultCenter] on location update
*
* They can be received with:
* [[NSNotificationCenter defaultCenter] addObserver:self
* selector:@selector(requestFinishedHandler:)
* name:kDidUpdateLocation
* name:kLocationManagerDidUpdateLocation
* object:nil];
*
***************************************************************************************/

extern NSString * const kLocationManagerDidUpdateLocation;
//Responding to Location Events
extern NSString * const kLocationManagerDidUpdateLocations;
extern NSString * const kLocationManagerDidFailWithError;
extern NSString * const kLocationManagerDidFinishDeferredUpdatesWithError;
//Pausing Location Updates
extern NSString * const kLocationManagerDidPauseLocationUpdates;
extern NSString * const kLocationManagerDidResumeLocationUpdates;
//Responding to Heading Events
extern NSString * const kLocationManagerDidUpdateHeading;
//Responding to Region Events
extern NSString * const kLocationManagerDidEnterRegion;
extern NSString * const kLocationManagerDidExitRegion;
extern NSString * const kLocationManagerDidDetermineStateForRegion;
extern NSString * const kLocationManagerMonitoringDidFailForRegion;
extern NSString * const kLocationManagerDidStartMonitoringForRegion;
//Responding to Ranging Events
extern NSString * const kLocationManagerDidRangeBeaconsInRegion;
extern NSString * const kLocationManagerRangingBeaconsDidFailForRegion;
//Responding to Authorization Changes
extern NSString * const kLocationManagerDidChangeAuthorizationStatus;

extern const CLLocationAccuracy kDefaultLocationAccuracy;
extern const NSTimeInterval kLocationTimerIntervalDefault;
Expand All @@ -28,5 +46,6 @@ extern const NSTimeInterval kLocationTimerIntervalDefault;
+ (instancetype)sharedInstance;

- (void)setTimerInterval:(NSTimeInterval)interval;
- (void)setCalibrationFlag:(BOOL)calibrationFlag;

@end
82 changes: 80 additions & 2 deletions SJHLocationManager/SJHLocationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,26 @@

#import "SJHRegionWrapper.h"

NSString * const kLocationManagerDidUpdateLocation = @"LocationManagerDidUpdateLocation";
//Responding to Location Events
NSString * const kLocationManagerDidUpdateLocations = @"LocationManagerDidUpdateLocation";
NSString * const kLocationManagerDidFailWithError = @"LocationManagerDidFailWithError";
NSString * const kLocationManagerDidFinishDeferredUpdatesWithError = @"LocationManagerDidFinishDeferredUpdatesWithError";
//Pausing Location Updates
NSString * const kLocationManagerDidPauseLocationUpdates = @"LocationManagerDidPauseLocationUpdates";
NSString * const kLocationManagerDidResumeLocationUpdates = @"LocationManagerDidResumeLocationUpdates";
//Responding to Heading Events
NSString * const kLocationManagerDidUpdateHeading = @"LocationManagerDidUpdateHeading";
//Responding to Region Events
NSString * const kLocationManagerDidEnterRegion = @"LocationManagerDidEnterRegion";
NSString * const kLocationManagerDidExitRegion = @"LocationManagerDidExitRegion";
NSString * const kLocationManagerDidDetermineStateForRegion = @"LocationManagerDidDetermineStateForRegion";
NSString * const kLocationManagerMonitoringDidFailForRegion = @"LocationManagerMonitoringDidFailForRegion";
NSString * const kLocationManagerDidStartMonitoringForRegion = @"LocationManagerDidStartMonitoringForRegion";
//Responding to Ranging Events
NSString * const kLocationManagerDidRangeBeaconsInRegion = @"LocationManagerDidRangeBeaconsInRegion";
NSString * const kLocationManagerRangingBeaconsDidFailForRegion = @"LocationManagerRangingBeaconsDidFailForRegion";
//Responding to Authorization Changes
NSString * const kLocationManagerDidChangeAuthorizationStatus = @"LocationManagerDidChangeAuthorizationStatus";

const CLLocationAccuracy kDefaultLocationAccuracy = 50.0;
const NSTimeInterval kLocationTimerIntervalDefault = 300.0;
Expand All @@ -36,6 +51,7 @@ @interface SJHLocationManager () <CLLocationManagerDelegate>
@property (strong, nonatomic) NSMutableSet *regionSet;
@property (strong, nonatomic) NSTimer *regionTimer;
@property NSTimeInterval regionTimerInterval;
@property BOOL shouldCalibrate;

@end

Expand All @@ -55,6 +71,7 @@ - (instancetype)init {
if (self) {
self.delegate = self;
self.isUpdatingLocation = NO;
_shouldCalibrate = NO;

self.regionSet = [[NSMutableSet alloc] init];
self.regionTimerInterval = kLocationTimerIntervalDefault;
Expand All @@ -80,6 +97,11 @@ - (void)setTimerInterval:(NSTimeInterval)interval {
}
}

#pragma mark - Calibration
- (void)setCalibrationFlag:(BOOL)calibrationFlag {
_shouldCalibrate = calibrationFlag;
}

#pragma mark - Location Methods
- (BOOL)isAuthorizedForLocationServices {
return [CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized;
Expand Down Expand Up @@ -181,14 +203,15 @@ - (void)sendRegionNotifications {
}

#pragma mark - location delegate methods
//Responding to Location Events
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations lastObject];

if ([self shouldstartLocationMonitoring] && [location horizontalAccuracy] <= [self desiredAccuracy]) {
[self stopUpdatingLocation];
NSLog(@"Location updated: %@", location);
if (self.isUpdatingLocation) {
[[NSNotificationCenter defaultCenter] postNotificationName:kLocationManagerDidUpdateLocation object:self userInfo:@{@"location": location}];
[[NSNotificationCenter defaultCenter] postNotificationName:kLocationManagerDidUpdateLocations object:self userInfo:@{@"location": location}];
}
if ([self.regionSet count] > 0) {
[self sendRegionNotifications];
Expand All @@ -201,6 +224,34 @@ - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *
[[NSNotificationCenter defaultCenter] postNotificationName:kLocationManagerDidFailWithError object:self userInfo:@{@"error": error}];
}

- (void)locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(NSError *)error {
NSLog(@"Location manager did finish deferred updates with error: %@", [error localizedDescription]);
[[NSNotificationCenter defaultCenter] postNotificationName:kLocationManagerDidFinishDeferredUpdatesWithError object:self userInfo:@{@"error": error}];
}

//Pausing Location Updates
- (void)locationManagerDidPauseLocationUpdates:(CLLocationManager *)manager {
NSLog(@"Location manager did pause location updates");
[[NSNotificationCenter defaultCenter] postNotificationName:kLocationManagerDidPauseLocationUpdates object:self userInfo:nil];
}

- (void)locationManagerDidResumeLocationUpdates:(CLLocationManager *)manager {
NSLog(@"Location manager did resume location updates");
[[NSNotificationCenter defaultCenter] postNotificationName:kLocationManagerDidResumeLocationUpdates object:self userInfo:nil];
}

//Responding to Heading Events
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
NSLog(@"Location manager did update heading: %@", newHeading);
[[NSNotificationCenter defaultCenter] postNotificationName:kLocationManagerDidUpdateHeading object:self userInfo:@{@"heading": newHeading}];
}

- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager {
NSLog(@"Location manager should display heading calibration. Should Calibrate: %@", (_shouldCalibrate ? @"YES" : @"NO"));
return _shouldCalibrate;
}

//Responding to Region Events
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(@"Entered region: %@", region);
[[NSNotificationCenter defaultCenter] postNotificationName:kLocationManagerDidEnterRegion object:self userInfo:@{@"region": region}];
Expand All @@ -213,9 +264,36 @@ - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)r
[super stopUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
NSLog(@"Did determine state: %@ for region: %@", [NSNumber numberWithInteger:state], region);
[[NSNotificationCenter defaultCenter] postNotificationName:kLocationManagerDidDetermineStateForRegion object:self userInfo:@{@"state": [NSNumber numberWithInteger:state], @"region": region}];
}

- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error {
NSLog(@"Monitoring did fail for region: %@, with error: %@", region, error);
[[NSNotificationCenter defaultCenter] postNotificationName:kLocationManagerMonitoringDidFailForRegion object:self userInfo:@{@"region": region, @"error": error}];
}

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
NSLog(@"Location manager did start monitoring for region: %@", region);
[[NSNotificationCenter defaultCenter] postNotificationName:kLocationManagerDidStartMonitoringForRegion object:self userInfo:@{@"region": region}];
}

//Responding to Ranging Events
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
NSLog(@"Location manager did range beacons: %@ in region: %@", beacons, region);
[[NSNotificationCenter defaultCenter] postNotificationName:kLocationManagerDidRangeBeaconsInRegion object:self userInfo:@{@"beacons": beacons, @"region": region}];
}

- (void)locationManager:(CLLocationManager *)manager rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region withError:(NSError *)error {
NSLog(@"Location manager ranging beacons did fail for region: %@ with error: %@", region, [error localizedDescription]);
[[NSNotificationCenter defaultCenter] postNotificationName:kLocationManagerRangingBeaconsDidFailForRegion object:self userInfo:@{@"region": region, @"error": error}];
}

//Responding to Authorization Changes
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
NSLog(@"Location manager did change authorization status: %@", [NSNumber numberWithInteger:status]);
[[NSNotificationCenter defaultCenter] postNotificationName:kLocationManagerDidChangeAuthorizationStatus object:self userInfo:@{@"status": [NSNumber numberWithInteger:status]}];
}

@end

0 comments on commit d36f1a7

Please sign in to comment.