Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to implement CLLocationManager

I have an app with a tab bar and 3 tabs. The current location of the user is going to be needed to be known on any of the three tabs. Would the best place to implement CLLocationManager be in the app delegate in this case?

Is it ok (good practise?) to put the CLLocationManager delegate methods in the app delegate m file?

Where would you suggest i place the CLLocationManager as I'm going to be calling -startUpdatingLocation from any of the three tabs?

Thanks

like image 710
joec Avatar asked Dec 07 '09 19:12

joec


People also ask

What is CLLocationManager in Swift?

Overview. A CLLocationManager object is the central place to manage your app's location-related behaviors. Use a location-manager object to configure, start, and stop location services. You might use these services to: Track large or small changes in the user's current location with a configurable degree of accuracy.

How do I turn on location services in Swift?

How to Turn On Location Services on Android. Location Services are turned on during the setup of your Android device, but you can also turn them on later by doing this: Tap Settings > Location. Move the slider to On.

What is cllocationcoordinate2d?

The latitude and longitude associated with a location, specified using the WGS 84 reference frame.


1 Answers

The app delegate is a reasonable place to put it. Another option would be to create a custom singleton factory class that has a class method that returns your location manager delegate and implement the delegate methods there. That would keep your app delegate class cleaner.

Here's a skeleton singleton class implemention based off of Peter Hosey's "Singletons in Cocoa: Doing them wrong". This may be overkill, but it's a start. Add your delegate methods at the end.

static MyCLLocationManagerDelegate *sharedInstance = nil; 

+ (void)initialize {
    if (sharedInstance == nil)
        sharedInstance = [[self alloc] init];
}

+ (id)sharedMyCLLocationManagerDelegate {
    //Already set by +initialize.
    return sharedInstance;
}

+ (id)allocWithZone:(NSZone*)zone {
    //Usually already set by +initialize.
    @synchronized(self) {
        if (sharedInstance) {
            //The caller expects to receive a new object, so implicitly retain it
            //to balance out the eventual release message.
            return [sharedInstance retain];
        } else {
            //When not already set, +initialize is our caller.
            //It's creating the shared instance, let this go through.
            return [super allocWithZone:zone];
        }
    }
}

- (id)init {
    //If sharedInstance is nil, +initialize is our caller, so initialze the instance.
    //If it is not nil, simply return the instance without re-initializing it.
    if (sharedInstance == nil) {
        if ((self = [super init])) {
            //Initialize the instance here.
        }
    }
    return self;
}

- (id)copyWithZone:(NSZone*)zone {
    return self;
}
- (id)retain {
    return self;
}
- (unsigned)retainCount {
    return UINT_MAX; // denotes an object that cannot be released
}
- (void)release {
    // do nothing 
}
- (id)autorelease {
    return self;
}
#pragma mark -
#pragma mark CLLLocationManagerDelegateMethods go here...
like image 143
Jason Jenkins Avatar answered Sep 21 '22 13:09

Jason Jenkins