1

I'm using iOS8 [8.0.2] and no matter what I try I cant get the CLLocationManager to work.

I added the following lines to info.plist.

<key>NSLocationWhenInUseUsageDescription</key>
<string>This app use location services.</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>This app use location services.</string>

In my implementation file :

In ViewDidLoad : 

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

But none of the below methods are being called.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

What is that I'm missing?

halfer
  • 19,824
  • 17
  • 99
  • 186
Sreejith
  • 1,345
  • 13
  • 25

2 Answers2

0

Check this post.

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

The above delegate was deprecated in iOS 6. Used the following instead:

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

EDIT

Do not set mapView.showUserLocation to YES before receiving authorization via locationManager:didUpdateLocations:

Set it this way:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    self.mapView.showUserLocation = YES;
    [locationManager stopUpdatingLocation];
}
Community
  • 1
  • 1
Rick
  • 1,818
  • 1
  • 15
  • 18
  • Did you add Core Location framework to your project? – Rick Oct 22 '14 at 15:11
  • Yes.. I did. I deleted the app and when I ran, it gave me this warning : Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first. – Sreejith Oct 22 '14 at 15:14
  • Did you add CLLocationManagerDelegate to @interface in .h file? – Rick Oct 22 '14 at 15:18
  • Also, set `mapView.showUserLocation = YES` only in `locationManager:didUpdateLocations:`, not before authorization is received. – Rick Oct 22 '14 at 15:24
  • Tried that. No change. – Sreejith Oct 22 '14 at 15:33
  • Try to run your code on device also check your app name under privacy->Location Service – mhrrt Oct 22 '14 at 17:20
0

I was in the same situation, and this answer brought me to the solution after struggling for a long time.

You need to make locationManager a property of your controller.

I should note that this occurred to me with Swift.

Community
  • 1
  • 1
hstove
  • 1
  • 1