3

My Xcode app that I am developing needs to get the latitude and longitude of the users iOS device. Although currently all I get is 0 for both values. It does not ask for permission to have the location, and I am on a real device not a simulator.

NSLocationAlwaysUsageDescription is in my info.plist

I have also imported CoreLocation and in my .h file

@interface LocationViewController : UIViewController <CLLocationManagerDelegate>

in my .m file

@interface LocationViewController () <CLLocationManagerDelegate>

Here is my code:

@implementation LocationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getCurrentLocation];
}

-(CLLocationCoordinate2D) getLocation{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];
    return coordinate;
}

- (void)getCurrentLocation{
    CLLocationCoordinate2D coordinate = [self getLocation];
    NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
    NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
    NSLog(@"Latitude  = %@", latitude);
    NSLog(@"Longitude = %@", longitude);
}
dandan78
  • 13,328
  • 13
  • 64
  • 78

2 Answers2

2

Put below before startUpdatingLocation

#ifdef __IPHONE_8_0
if(IS_OS_8_OR_LATER) {
     // Use one or the other, not both. Depending on what you put in info.plist
    [self.locationManager requestWhenInUseAuthorization];
}
#endif
[self.locationManager startUpdatingLocation];

Also call [self getCurrentLocation]; in viewDidLoad & viewDidAppear both.

It will work.

Also make sure you have entry for requestWhenInUseAuthorization in info.plist file too.

check this for more info

Community
  • 1
  • 1
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • Tried it and got an error on the if statement. I commented out the if statement and kept [self.locationManager requestWhenInUseAuthorization]; but still returns 0. Also put as you said in viewdidload/viewdidappear – Dan Winter-Wijntjes Mar 26 '15 at 06:52
  • @DanWinter-Wijntjes : check [here](http://stackoverflow.com/questions/26356558/mapkit-giving-wrong-lat-lang-ios-8/26361534#26361534) what I am doing.. It will work... – Fahim Parkar Mar 26 '15 at 06:59
2

Refer this answer. Hope, this helps.

Make this changes :

-(CLLocationCoordinate2D) getLocation{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager requestWhenInUseAuthorization];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
return coordinate;
}

 -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
     NSLog(@"Status : %d", status);
}

Goto Settings > Privacy > Location > Your App > Always

And see how 'Status' value gets changes.

Community
  • 1
  • 1
itsji10dra
  • 4,603
  • 3
  • 39
  • 59