Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLLocationManager Delegate methods are not getting called

I am using CLLocationManager class. I have a simple class method for capturing the location

+(void)captureLocation{
    mLocationManager = [[CLLocationManager alloc]init];
    mLocationManager.delegate = (id<CLLocationManagerDelegate>)self;
    mLocationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [mLocationManager startUpdatingLocation];
}

and i have the delegate methods of CLLocationManager also

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation   *)newLocation fromLocation:(CLLocation *)oldLocation
{
}

now i am trying to call this method in my viewDidLoad as

- (void)viewDidLoad
{
    [super viewDidLoad];
    [myclass captureLocation];
}

the method is getting called but the delegate methods are not getting called.

I have other class method also and from there if I try to call the method again the captureLocation method is getting called but the delegate methods are not called. Here is the other class method

+(void)initialize{
    [self captureLocation];
}

please help me to find out why delegate methods are not getting called as I am new to this field. Thanks in advance.

like image 238
Newbee Avatar asked Nov 19 '13 12:11

Newbee


2 Answers

Also know that CoreLocation permissions have changed with iOS 8. If you don't request the new permissions authorizations, CoreLocation doesn't do anything. It fails quietly because the delegate methods are never called.

I realize this question was asked in 2013, but if you are having a similar problem with the delegate methods not getting called, this article is extremely helpful:

http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

While the article is very detailed and long, the actual code fix can be as minor as this:

if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [locationManager requestWhenInUseAuthorization];
}

And you have to add a value to info.plist, which is the message to display in the permissions alert. See screen grab.

Screen grab of new entry in info.plist

Key: NSLocationWhenInUseUsageDescription

Value: Location is required to find out where you are (You can change that to whatever you want)

like image 177
Hugh Avatar answered Nov 16 '22 12:11

Hugh


You are setting the delegate of the CLLocationManager inside a class method (i.e. one prefixed by + rather than -). So, when you reference self within that class method, that's the class, not an instance of the class. So, you are trying to set the delegate to the class rather than an instance of the class.

That won't work. The delegate methods are instance methods, not class methods. (This is presumably why you had to use the CLLocationManagerDelegate cast when assigning the delegate.)

You must actually instantiate whichever class you've implemented the CLLocationManagerDelegate methods. If you don't want to tie that instance to a particular view controller, you could use a singleton pattern. Regardless, you can set the location manager's delegate to point to that instance of that class.


For example, if you wanted it to be a singleton:

//  MyClass.h

#import <Foundation/Foundation.h>

@interface MyClass : NSObject

+ (instancetype)sharedManager;
- (void)startCapture;
- (void)stopCapture;

@end

and

//  MyClass.m

#import "MyClass.h"
#import <CoreLocation/CoreLocation.h>

@interface MyClass () <CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;

@end

@implementation MyClass

+ (instancetype)sharedManager
{
    static id sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
    });
    return sharedMyManager;
}

- (void)startCapture
{
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];
}

- (void)stopCapture
{
    [self.locationManager stopUpdatingLocation];
    self.locationManager = nil;
}

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

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    // ...
}

@end

And then,

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[MyClass sharedInstance] startCapture];
}
like image 20
Rob Avatar answered Nov 16 '22 11:11

Rob