Skip to content

Instantly share code, notes, and snippets.

@justin
Created December 5, 2010 16:27
Show Gist options
  • Save justin/729217 to your computer and use it in GitHub Desktop.
Save justin/729217 to your computer and use it in GitHub Desktop.
SGLocationManager's Meat
//
// SGLocationManager.h
// Second Gear Pitbox
//
// Created by Justin Williams on 12/5/10.
// Copyright 2010 Second Gear. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
typedef void (^SGLocationManagerHandler)(CLLocation *location, NSError *error);
@interface SGLocationManager : NSObject <CLLocationManagerDelegate>
{
@private
CLLocationManager *locationManager;
SGLocationManagerHandler handler;
}
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, copy) SGLocationManagerHandler handler;
- (void)startUpdatingLocationWithCompletionHandler:(SGLocationManagerHandler)handler;
- (void)stopUpdatingLocation;
@end
//
// SGLocationManager.m
// Second Gear Pitbox
//
// Created by Justin Williams on 12/5/10.
// Copyright 2010 Second Gear. All rights reserved.
//
#import "SGLocationManager.h"
@implementation SGLocationManager
@synthesize locationManager;
@synthesize handler;
- (id) init
{
if ((self = [super init]))
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
}
return self;
}
#pragma mark -
#pragma mark Instance Methods
// +--------------------------------------------------------------------
// | Instance Methods
// +--------------------------------------------------------------------
- (void)startUpdatingLocationWithCompletionHandler:(SGLocationManagerHandler)theHandler
{
if ((self.locationManager.locationServicesEnabled))
{
self.handler = theHandler;
[self.locationManager startUpdatingLocation];
}
else
{
NSRunAlertPanel(NSLocalizedString(@"Location Services Disabled", nil), NSLocalizedString(@"I can't figure out where you are if you don't turn on location services.", nil), NSLocalizedString(@"OK", nil), nil, nil);
}
}
- (void)stopUpdatingLocation
{
self.handler = nil;
[self.locationManager stopUpdatingLocation];
}
#pragma mark -
#pragma mark CLLocationManager Delegate Methods
// +--------------------------------------------------------------------
// | CLLocationManager Delegate Methods
// +--------------------------------------------------------------------
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;
{
self.handler(newLocation, nil);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;
{
self.handler(nil, error);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment