0

Core Location is not calling didUpdateLocations. I have 2 classes involved LocationManager and a View Controller. Plist is set for requestAlwaysAuthorization. Location is simulated in debug. Can anyone help me spot the error?

LocationManager.h

@interface LPLocationManager : NSObject <CLLocationManagerDelegate>

+(LPLocationManager*)sharedManager;

@property (strong, atomic) CLLocationManager *locationManager;
@property (nonatomic, retain) CLLocation *location;
@end

LocationManager.m

+(LPLocationManager*)sharedManager{
    static LPLocationManager *sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[LPLocationManager alloc]init];
    });

    return sharedManager;
}

- (id)init
{
    self = [super init];
    if (self) {
        self.locationManager = [[CLLocationManager alloc]init];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = 10;

    }

    return self;
}

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


    self.location = [locations lastObject];

    [self setCurrentLocation:self.location];

    NSLog(@"self.location in didupdatelocation %@", self.location);

    [self.locationManager stopUpdatingLocation];

}

ViewController.m (where startUpdating is called)

- (void)refresh:(UIRefreshControl *)refreshControl {


    LPLocationManager *locationObject = [LPLocationManager sharedManager];
    NSLog(@"location object %@", locationObject);
    [locationObject.locationManager requestAlwaysAuthorization];
    NSLog(@"locationManager %@", locationObject.locationManager);
    [locationObject.locationManager startUpdatingLocation];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(getLocation:) name:@"locationNotification" object:nil];

    [refreshControl endRefreshing];

}
noobsmcgoobs
  • 2,716
  • 5
  • 32
  • 52

2 Answers2

4

Figured it out.

In simulator, go to Settings -> General and scroll to Reset. Click Reset Location & Privacy.

Close simulator and rerun app. Back in Settings, go to Privacy -> Location and select Always for the app.

noobsmcgoobs
  • 2,716
  • 5
  • 32
  • 52
  • It shows the prompt which asks for authorization but -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error gets called immedietly and the error i get is Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)" – Neeraj Oct 15 '14 at 05:19
  • And you tried this? http://stackoverflow.com/questions/24062509/ios-8-location-services-not-working – noobsmcgoobs Oct 15 '14 at 05:19
  • yes, i added NSLocationAlwaysUsageDescription in info.plist and also called if ([ClLM respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [ClLM requestWhenInUseAuthorization]; } – Neeraj Oct 15 '14 at 05:21
  • i am checking in simulator – Neeraj Oct 15 '14 at 05:22
  • it has also shown the prompted the message that i used with NSLocationAlwaysUsageDescription in info.plist – Neeraj Oct 15 '14 at 05:23
  • http://stackoverflow.com/questions/6032976/didfailwitherror-error-domain-kclerrordomain-code-0-the-operation-couldn-t-be check out this answer - a lot of different answers. this happened to me but I can't remember what the fix was – noobsmcgoobs Oct 15 '14 at 05:23
  • i forgot to restart simulator earlier, sorry – Neeraj Oct 15 '14 at 05:33
  • The other way to fix it is to (1)Start the app. (2)Let simulator with app load. (3)Set location (4)Un-set location (5)Reset location – noobsmcgoobs Dec 05 '14 at 04:56
1

add these key-values in your info.plist file

<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSLocationAlwaysUsageDescription</key>
<string></string>

after this define this macro

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

and put this code in viewWillAppear

locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    if(IS_OS_8_OR_LATER) {
        [locationManager requestAlwaysAuthorization];
    }

[locationManager startUpdatingLocation];
sanjeet
  • 1,539
  • 15
  • 37