Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current Location returning 0

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);
}
like image 759
Dan Winter-Wijntjes Avatar asked Dec 10 '25 00:12

Dan Winter-Wijntjes


2 Answers

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.

like image 152
itsji10dra Avatar answered Dec 14 '25 21:12

itsji10dra


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

like image 23
Fahim Parkar Avatar answered Dec 14 '25 20:12

Fahim Parkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!