Is it OK to put the CLLocationManager Delegate methods in a Singleton class which is a subclass of NSObject instead of UIViewController? I would like to do this as Id like to invoke the Singleton from the App Delegate and start getting the coordinates while the UI is loading
I have a locationcontroller class and I have the following initialization code in it
static locationController *sharedLocController = NULL;
+(locationController *) getSharedController
{
if (sharedLocController !=nil)
{
NSLog(@"locationController has already been created.....");
return sharedLocController;
}
@synchronized(self)
{
if (sharedLocController == nil)
{
sharedLocController = [[self alloc] init];
}
}
return sharedLocController;
}
//==============================================================================
+(id)alloc
{
@synchronized([locationController class])
{
NSAssert(sharedLocController == nil, @"Attempted to allocate a second instance of a sharedLocMgr singleton.");
sharedLocController = [super alloc];
return sharedLocController;
}
return nil;
}
//==============================================================================
-(id)init
{
self = [super init];
if(sharedLocController !=nil)
{
if(!self.locMgr)
{
[self initLocationManager];
}
}
return sharedLocController;
}
//==============================================================================
-(void)initLocationManager
{
self.locMgr = [[CLLocationManager alloc] init];
self.locMgr.delegate = self;
self.locMgr.distanceFilter = kCLDistanceFilterNone;
self.locMgr.desiredAccuracy = kCLLocationAccuracyBest;
[self.locMgr startUpdatingLocation];
NSLog(@"location manager object %@", locMgr);
}
Problem is that the self.locMgr object is always null.
Thanks
Get rid of your alloc function. It isn’t necessary and is returning nil, which means this init call is getting called on a nil pointer: