3

I'm pretty new to iOS development (my first app) and I faced this issue.

I have an iPhone app that should get user's current location in multiple ViewControllers upon user button touch. To prevent redundant code (implementing locationManager:didFailWithError, locationManager:didUpdateToLocation:fromLocation, etc. multiple times in different view controllers) I decided to create a custom class called LocationManager:

LocationManager.h

@interface LocationManager : NSObject <CLLocationManagerDelegate> {
@private
    CLLocationManager *CLLocationManagerInstance;
    id<LocationManagerAssigneeProtocol> assignee;
}

-(void) getUserLocationWithDelegate:(id) delegate;

LocationManager.m

@implementation LocationManager

-(id)init {
    self = [super init];
    if(self) {
        CLLocationManagerInstance = [[CLLocationManager alloc] init];
        CLLocationManagerInstance.desiredAccuracy = kCLLocationAccuracyBest;
        CLLocationManagerInstance.delegate = self;
    }
    return self;
}

-(void) getUserLocationWithDelegate:(id) delegate {
    if([CLLocationManager locationServicesEnabled]) {
        assignee = delegate;
        [CLLocationManagerInstance startUpdatingLocation];
    }
}

#pragma CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
...
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    [CLLocationManagerInstance stopUpdatingLocation];
    [assignee didUpdateToLocation:newLocation];
}

and I have a protocol called LocationManagerAssigneeProtocol that my ViewControllers implement

@protocol LocationManagerAssigneeProtocol <NSObject>

@required
-(void) didUpdateToLocation:(CLLocation *) location;

@end

and in my viewcontroller where needed

- (IBAction)getMyLocation:(id)sender {

    [locationMgr getUserLocationWithDelegate:self];
}

This code works perfectly, however, I have a feeling that I'm violating some design patterns here by letting LocationManager be able to call a function of the class that itself initiated a call to Location Manager. On the other hand, I don't want to go with implementing CLLocationManagerDelegate for all my viewcontrollers that are supposed to work with locations.

Are there any better solution to this issue?

NarbehM
  • 345
  • 2
  • 13
  • I'm not sure I follow your concern about "letting LocationManager be able to call a function of the class that itself initiated a call". You are using a delegate protocol to do so if I'm not mistaken, which is desirable. – Carl Veazey Jan 11 '13 at 08:50
  • I think that's fine, that's how delegation works, you don't have a dependency on the class though only on the protocol. The only thing I'd suggest is that perhaps you may want to switch to a notification based system if you have multiple view controllers depending on the same instance for location updates. – Carl Veazey Jan 11 '13 at 09:00
  • @CarlVeazey Would you please bring an example of what would you change to make it Notification based? – NarbehM Jan 11 '13 at 09:34
  • why do you not create a base `UIViewController` with conforming the `CLLocationManagerDelegate` protocol, and why you do not inherit your custom `UIViewController` classes of this base-controller? – holex Jan 11 '13 at 12:12

1 Answers1

2

I agree with @CarlVeazey on this one. Delegate are great for a 1 to 1 relationship existing at any one time, however in your case it seems that you may need multiple viewControllers to respond to location events at any given time. So just remove anything related to your delegate and its associated protocol.

I'd probably make LocationManager class a singleton and modify the methods for updating:

+(LocationManager *)sharedInstance
{
    static LocationManager *_sharedInstance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _sharedInstance = [[self alloc] init];
    });

    return _sharedInstance;
}

-(void)getUserLocation
{
    if ([CLLocationManager locationServicesEnabled])
        [CLLocationManager startUpdatingLocation];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
    [CLLocationManagerInstance stopUpdatingLocation];
    [[NSNotificationCenter defaultCenter] postNotificationWithName:@"LocationManagerDidUpdateLocation" object:newLocation];
}

... Then any viewController that needs to use this class would have something like:

-(void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserverForName:@"LocationManagerDidUpdateLocation" object:self queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        CLLocation *location = note.object;
        ...
    }];
}

-(IBAction)getMyLocation:(id)sender {
    [[LocationManager sharedInstance] getUserLocation];
}

Hope that helps and makes sense.

Shaps
  • 91
  • 8