Search code examples
iosobjective-ciphonecore-location

didUpdateLocations called only once and then stops firing


I want to make a simple IOS 6.0 application that shows the lat/lon on screen each time the location is changed. The ViewController files are pretty trivial.

I alloc a CCLocationManager objec, set its variables and start it. didUpdateLocations is called once or twice and then it stops being fired, even if the iPhone is moved. If I press Home button and reopen the app, the data on screen is updated once or twice before it stops again. At simulation it works fine but not on a real 3GS iPhone.

If I uncomment the start/stop inside didUpdateLocations and continuously stop and start the service, it works, but the battery gets drained in extreme rates.

Also, this is part of a much bigger project and didUpdateLocations must be called each time the location is changed.

ViewController.h

#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController
@property (nonatomic , strong) CLLocationManager *locationManager;
@property (weak, nonatomic) IBOutlet UILabel *lbl;
@end

ViewConroller.m

#import "ViewController.h" @interface ViewController ()

@end

@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _locationManager = [[CLLocationManager alloc] init]; _locationManager.delegate = self; _locationManager.distanceFilter = kCLHeadingFilterNone; // whenever we move _locationManager.desiredAccuracy = kCLLocationAccuracyBest; _locationManager.PausesLocationUpdatesAutomatically = NO; [_locationManager startUpdatingLocation]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation *newLocation = [locations lastObject]; _lbl.text=[NSString stringWithFormat:@"%lf %lf",newLocation.coordinate.latitude, newLocation.coordinate.longitude]; //[_locationManager stopUpdatingLocation]; //[_locationManager startUpdatingLocation]; } @end

If there's any advice on what's wrong, I would welcome it, I have lost a week already without solving it.

BTW, I have tested various values for the _locationManager variables but without any chnage in the behaviour

Additional info: application is authorized to use location services application is in foreground


Solution

  • Few tips for similar issues as per my experience with location-based programming on iOS.

    • If it works on the simulator, then it should work on the actual device as well.
    • Unlike the simulator, the actual device will only give an update when it receives new information from GPS (you need to move physically).
    • There are many cases where you won't get enough/required updates on a device, like when you are in a building (or covered area).
    • Try testing the app in open space (GPS works much better in open space rather than in streets, corridors, and courtyards surrounded by buildings).

    PS. I don't know how much you moved but just to let you know, moving a few steps doesn't make sure you will get a new location update, try using it on some vehicles (bike/car/bus). Moreover, setting the distance filter to kCLDistanceFilterNone will help with testing, but it can consume 100% battery in 2-3 hours.