Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLLocationManager startUpdatingLocation not calling locationManager:didUpdateLocations: or locationManager:didFailWithError:

I'm trying to use the CLLocationManager framework in my iOS project to access the user's location but when I call

[locationManager startUpdatingLocation] 

neither locationManager:didUpdateLocations: or locationManager:didFailWithError: are getting called.

//myViewController.h @interface myViewController : UITableViewController <CLLocationManagerDelegate> @end  //myViewController.m @implementation myViewController{     CLLocationManager *locationManager; }  //edit - (void)viewDidLoad { [super viewDidLoad]; locationManager = [[CLLocationManager alloc] init]; } //finish edit  -(void)getLocation {     locationManager.delegate = self;     locationManager.desiredAccuracy = kCLLocationAccuracyBest;     [locationManager startUpdatingLocation]; }  - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { UIAlertView *errorAlert = [[UIAlertView alloc]                            initWithTitle:@"Error"                                   message:@"Failed to Get Your Location"                                               delegate:nil                         cancelButtonTitle:@"OK"                        otherButtonTitles:nil]; [errorAlert show]; }  - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {     CLLocation *newLocation = locations[[locations count] -1];     CLLocation *currentLocation = newLocation;     NSString *longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];     NSString *latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];  if (currentLocation != nil) {    NSLog(@"latitude: %@", latitude);    NSLog(@"longitude: @"%@", longitude); }else {     UIAlertView *errorAlert = [[UIAlertView alloc]                                initWithTitle:@"Error" message:@"Failed to Get Your Location"                                                       delegate:nil                                              cancelButtonTitle:@"OK"                                              otherButtonTitles:nil];     [errorAlert show]; }  } @end 

Neither delegate method is being called despite what it says in the documentation:

"This method returns immediately. Calling this method causes the location manager to obtain an initial location fix (which may take several seconds) and notify your delegate by calling its locationManager:didUpdateLocations: method [...] In addition to your delegate object implementing the locationManager:didUpdateLocations: method, it should also implement the locationManager:didFailWithError: method to respond to potential errors."

Don't know how to debug the issue.

Thanks,

JA

like image 227
ja. Avatar asked Sep 18 '14 15:09

ja.


2 Answers

enter image description hereJust add this in info.plist

NSLocationAlwaysUsageDescription --- I need Location

NSLocationWhenInUseUsageDescription --- I need Location

privacy - location usage description --- I need Location

  locationManager = [[CLLocationManager alloc] init];   locationManager.delegate=self;   locationManager.desiredAccuracy=kCLLocationAccuracyBest;   locationManager.distanceFilter=kCLDistanceFilterNone;   [locationManager requestWhenInUseAuthorization];   [locationManager startMonitoringSignificantLocationChanges];   [locationManager startUpdatingLocation]; 

Now it will call your didUpdateToLocation definitely.

for more details click here

like image 109
Mohit Tomar Avatar answered Sep 28 '22 03:09

Mohit Tomar


You strongly need to check that you initialize CLLocationManager on main thread. In other case you will not get updateLocation event.

I can't find such info in Apple docs but it works for me anyway.

like image 35
malex Avatar answered Sep 28 '22 02:09

malex