danywarner danywarner - 6 months ago 18
iOS Question

Is there a way to know whether locationManager is active or stopped?

In my app, I am developing a CoreLocation functionality module and I need to know whether

startUpdatingLocation
or
stopUpdatingLocation
have been called.
I know I could use a boolean variable, but I wonder if there is something more 'globally' useful.

Answer

There is no API to check the "running" status for the CLLocationManager. You could wrap CLLocationManager in a class (or subclass it) and have "running" state on this custom class.

Something like this perhaps:

Swift:

class MyLocationManager: CLLocationManager {
    var running: Bool

    override init() {
        running = false
        super.init()
    }

    override func startUpdatingLocation() {
        super.startUpdatingLocation()
        running = true
    }

    override func stopUpdatingLocation() {
        super.stopUpdatingLocation()
        running = false
    }

}

ObjC:

@interface MyLocationManager : CLLocationManager
@property (nonatomic, assign) BOOL running;
@end

@implementation MyLocationManager
- (void) startUpdatingLocation
{
    [super startUpdatingLocation];
    self.running = YES;
}

- (void) stopUpdatingLocation
{
    [super stopUpdatingLocation];
    self.running = NO;
}
@end

PS: This class is not thread safe, but you get the idea