Skip to content

Instantly share code, notes, and snippets.

@kwylez
Created April 19, 2012 15:53
Show Gist options
  • Save kwylez/2421928 to your computer and use it in GitHub Desktop.
Save kwylez/2421928 to your computer and use it in GitHub Desktop.
Sample VC implementing shared location manager using KVO
- (void)dealloc {
[super dealloc];
[[CWCLocationManager sharedInstance] removeObserver:self
forKeyPath:@"currentLocation"];
}
- (id)init {
self = [super init];
if (self) {
}
return self;
}
- (void)viewDidUnload {
[super viewDidUnload];
[[CWCLocationManager shareInstance] removeObserver:self forKeyPath:@"currentLocation"];
}
- (void)viewDidLoad {
[super viewDidLoad];
CWCLocationManager *manager = [CWCLocationManager sharedInstance];
[manager addObserver:self
forKeyPath:@"currentLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
}
#pragma mark - Public Methods
- (void)callSuperCoolMethodThatNeedsCoordinate:(CLLocationCoordinate2D)coord {
// post something cool from location
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqual:@"currentLocation"]) {
if ([CWCLocationManager sharedInstance].currentLocation) {
[[CWCLocationManager sharedInstance] stop];
CLLocationCoordinate2D currentLoc = [CWCLocationManager sharedInstance].currentLocation.coordinate;
[self callSuperCoolMethodThatNeedsCoordinate:currentLoc];
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment