问题
I tried to get the user's latitude and longitude but I got result 0.000000 - I have tried following code.
- I have assign the location manager variable.
- Location Manager allocated and set the delegate is self.
- Checked the iOS 8 version validation requesting.
- Using location start update location function.
Code:
locationmanager=[[CLLocationManager alloc]init];
locationmanager.delegate=self;
// check before requesting, otherwise it might crash in older version
if ([locationmanager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationmanager requestWhenInUseAuthorization];
}
[locationmanager startUpdatingLocation];
#pragma mark - CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"locations %@",locations);
CLLocation*location = [locations lastObject];
NSLog(@"location %f",location.coordinate.latitude);
}
Result:2014-10-15 11:10:20.118 Good[714:25676] latitude 0.000000
回答1:
Make sure that you have added the following lines NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription in .plist file.
回答2:
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) - (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
if(IS_OS_8_OR_LATER){
NSUInteger code = [CLLocationManager authorizationStatus];
if (code == kCLAuthorizationStatusNotDetermined && ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) {
// choose one request according to your business.
if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
[self.locationManager requestAlwaysAuthorization];
} else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
[self.locationManager requestWhenInUseAuthorization];
} else {
NSLog(@"Info.plist does not contain NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription");
}
}
}
[self.locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
}
In iOS 8 you need to do two extra things to get location working: Add a key to your Info.plist and request authorization from the location manager asking it to start. There are two Info.plist keys for the new location authorization. One or both of these keys is required. If neither of the keys are there, you can call startUpdatingLocation but the location manager won’t actually start. It won’t send a failure message to the delegate either (since it never started, it can’t fail). It will also fail if you add one or both of the keys but forget to explicitly request authorization. So the first thing you need to do is to add one or both of the following keys to your Info.plist file:
- NSLocationWhenInUseUsageDescription
- NSLocationAlwaysUsageDescription
Both of these keys take a string
which is a description of why you need location services. You can enter a string like “Location is required to find out where you are” which, as in iOS 7, can be localized in the InfoPlist.strings file.

回答3:
In iOS 8, this code fails silently i.e you won’t get any error or warning.
You need to do two extra things to get location working:
- Add a key to your Info.plist
- Request authorization from the location manager asking it to start.
Any one or both of below keys needs to be added in Info.plist file.
- NSLocationWhenInUseUsageDescription
- NSLocationAlwaysUsageDescription
These are of String type and value can be any message description or empty.
Now you need to request authorisation for corresponding location method. Use any one of below calls :
- [self.locationManager requestWhenInUseAuthorization]
- [self.locationManager requestAlwaysAuthorization]
来源:https://stackoverflow.com/questions/26376151/not-able-to-get-the-users-location-in-ios-8-using-cllocation-manager