无法使用CLLocation Manager获取用户在iOS 8中的位置?

MOH*_*N.C 2 dictionary objective-c core-location cllocationmanager ios8

我试图获得用户的纬度和经度,但我得到了结果0.000000 - 我尝试了以下代码.

  • 我已经分配了位置管理器变量.
  • 位置管理器已分配并设置委托是自己的.
  • 检查了iOS 8版本验证请求.
  • 使用位置启动更新位置功能.

码:

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);
}   
Run Code Online (Sandbox Code Playgroud)

结果:2014-10-15 11:10:20.118好[714:25676]纬度0.000000

小智 5

确保在.plist文件中添加了以下行NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription.