CLLocation current location not working

杀马特。学长 韩版系。学妹 提交于 2020-01-07 02:26:08

问题


I have this code in my class ViewController:

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.


回答1:


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.

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

<key>NSLocationAlwaysUsageDescription</key>
<string>message for location uses</string>

<key>NSLocationWhenInUseUsageDescription</key>
<string>message for location uses</string>



回答2:


Try this code in viewDidLoad...

//---- For getting current gps location
    locationManager = [CLLocationManager new];
    locationManager.delegate = self;

    if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])  {

        [locationManager requestWhenInUseAuthorization];
    }

    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
    //------

You also have to add a string for the NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription keys to the app's Info.plist.




回答3:


locationManager =[[CLLocationManager alloc] init];
    [locationManager requestWhenInUseAuthorization];
    locationManager.delegate=self;

    locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];

you can add in your plist:

<key>NSLocationAlwaysUsageDescription</key>


<key>NSLocationWhenInUseUsageDescription</key>


来源:https://stackoverflow.com/questions/39344121/cllocation-current-location-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!