CLLocationManager Delegate methods are not getting called

不羁的心 提交于 2020-01-30 05:06:01

问题


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.


回答1:


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.

Key: NSLocationWhenInUseUsageDescription

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




回答2:


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];
}



回答3:


Calling self in a + method set your delegate to nil as it means ClassName as in [[ClassName alloc] init].

you need to:

mLocationManager.delegate = mLocationManager

instead of

mLocationManager.delegate (id<CLLocationManagerDelegate>)self;



回答4:


in ios6 locationManager:didUpdateToLocation:fromLocation: is deprecated so you need to add another method in your code ...

-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{

      // this method get called in ios7 . 
}



回答5:


In iOS -8 need to do some changes :

Please have a look into this :Get current location in iOS-7 and iOS-8



来源:https://stackoverflow.com/questions/20072231/cllocationmanager-delegate-methods-are-not-getting-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!