CLLocationManager Delegation Methods Not Called

I am using a class CLLocationManager. I have a simple class method to capture location

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

and I have delegate methods CLLocationManageralso

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

now I'm trying to call this method in my viewDidLoadhow

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

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

I have another class method, and from there, if I try to call the method again, the method captureLocationgets called, but the delegate methods are not called. Here is another class method

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

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

+4
source share
5

, CoreLocation iOS 8. , CoreLocation . , .

, 2013 , , , :

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

, , :

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

info.plist, . . .

Screen grab of new entry in info.plist

: NSLocationWhenInUseUsageDescription

: , , ( , )

+10

delegate CLLocationManager (.. +, -). , self , , . , delegate , .

. - , . (, , CLLocationManagerDelegate delegate.)

, CLLocationManagerDelegate. , . , , .


, , :

//  MyClass.h

#import <Foundation/Foundation.h>

@interface MyClass : NSObject

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

@end

//  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

,

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[MyClass sharedInstance] startCapture];
}
+5

self + , ClassName, [[ClassName alloc] init].

:

mLocationManager.delegate = mLocationManager

mLocationManager.delegate (id<CLLocationManagerDelegate>)self;
0
source

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

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

      // this method get called in ios7 . 
}
0
source

In iOS -8, you need to make some changes:

Please check out this: Get current location in iOS-7 and iOS-8

0
source

All Articles