KAR KAR - 4 months ago 29
iOS Question

CLLocation current location not working

I have this code in my class


CLLocationManager *locationManager;

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

locationManager = [[CLLocationManager alloc] init];
[locationManager requestWhenInUseAuthorization];

- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

[locationManager startUpdatingLocation];

#pragma mark - CLLocationManagerDelegate

-(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"Did finish with error - %@", error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to get your location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];

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

NSLog(@"did Update Location - %@", newLocation);
CLLocation *currentLocation = newLocation;

if(currentLocation != nil) {
_longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
_latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];

But I am not getting the location or the popup for allowing access.

I am using the Core Location framework.

On button click I am printing latitude, longitude and address in labels.

I am testing this on the simulator.


Sometimes Simulator wont work with location enabled services use Apple Device for perfect testing.

Add the following keys inside your info.plist file to get allow access popup.

enter image description here

or add them as updating info.plist file source code.

<string>message for location uses</string>

<string>message for location uses</string>