Ios – CoreLocation: didUpdateToLocation not getting called

core-locationiosobjective cxcode

I'm currently developing my first CoreLocation-based app and I have a problem with the CLLocationManager not calling delegate methods. Here's the class I'm using:

//
//  LocationGetter.h
//  whatsunderme
//
//  Created by Tobias Timpe on 16.06.12.
//  Copyright (c) 2012 tobisoft. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@protocol LocationGetterDelegate
@required
- (void) newPhysicalLocation:(CLLocation *)location;
@end

@interface LocationGetter : NSObject <CLLocationManagerDelegate> {
   CLLocationManager *locationManager;
   id delegate;
} 

- (void)startUpdates;

@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) id delegate;
@end

//
//  LocationGetter.m
//  whatsunderme
//
//  Created by Tobias Timpe on 16.06.12.
//  Copyright (c) 2012 tobisoft. All rights reserved.
//

#import "LocationGetter.h"

@implementation LocationGetter

@synthesize locationManager, delegate;
BOOL didUpdate = NO;

- (void)startUpdates {
    if (self.locationManager == nil) {
        self.locationManager = [[CLLocationManager alloc] init];
    }
    self.locationManager.delegate = self;
    NSLog(@"Starting Location Updates");
    locationManager.distanceFilter = 100;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"Error");
}

// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manage didUpdateToLocation:(CLLocation *)
newLocation fromLocation:(CLLocation *)oldLocation {
        NSLog(@"new Location found");
    didUpdate = YES;
    // Disable future updates to save power.
    [self.locationManager stopUpdatingLocation];

    [self.delegate newPhysicalLocation:newLocation];

}


@end

Neither the didUpdateToLocation nor the didFailWithError methods are getting called. I don't know why.
Hope you can help me with this since I've been trying to figure out what the problem is for 2 hours.

Best Answer

Use my code, it should work for you...

First ensure you are using the protocol by adding the following, for example to your AppDelegate or whichever controller you want CoreLocation to work in...

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>

Create some properties, I also use a custom method but you don't have too...

@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLLocation *currentLocation;

- (void)startUpdatingCurrentLocation;

Then in your implementation file call the custom method to start CL

// start location services
[self startUpdatingCurrentLocation];

Below is the code to my custom method.

- (void)startUpdatingCurrentLocation
{    
  // if location services are on
  if([CLLocationManager locationServicesEnabled])
  {
    // if location services are restricted do nothing
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted )
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Determining your current location cannot be performed at this time because location services are enabled but restricted." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    }
    else 
    {
        if(!locationManager_)
        {
            locationManager_ = [[CLLocationManager alloc] init];
            [locationManager_ setDesiredAccuracy:kCLLocationAccuracyBest];
            [locationManager_ setDelegate:self];
            [locationManager_ setDistanceFilter:5.0f];          // measured in meters
            [locationManager_ setHeadingFilter:5];              // measured in degrees
            [locationManager_ setPurpose:@"Enabling location services is required in order to automatically determine the location of yada yada yada..."];
        }

        [locationManager_ startUpdatingLocation];
    }
  }
  else 
  {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Determining your current location cannot be performed at this time because location services are not enabled." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
  }
}
Related Topic