【问题标题】:IOS-didFailWithError: Error Domain=kCLErrorDomain Code=0. (kCLErrorDomain error 0.)” [duplicate]IOS-didFailWithError:错误域=kCLErrorDomain 代码=0。 (kCLErrorDomain 错误 0。)” [重复]
【发布时间】:2016-07-23 18:56:10
【问题描述】:

我尝试获取用户当前位置的经度和纬度,我的代码就是这样:

- (void)viewDidLoad {
    self.locationManager=[[CLLocationManager alloc]init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    [locationManager startUpdatingLocation];
}

-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
    float latitude=newLocation.coordinate.latitude;
    float longitude=newLocation.coordinate.longitude;
    NSLog(@"%f",latitude);
    NSLog(@"%f",longitude);
    [locationManager stopUpdatingLocation];

}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{
    NSLog(@"didFailWithError: %@", error);
}

我总是从 didFailWithError 方法中得到这个错误:

didFailWithError: Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)" 

【问题讨论】:

  • 我重置了网络,但仍然无法正常工作。 @derpoliuk
  • @Mr.Bond 请检查您在模拟器中的位置 Debug>Location>Current Location
  • 那也行不通。@bhavin ramani
  • @Mr.Bond 你是在模拟器还是在xcode中设置了你的当前位置?
  • 您是否在项目的 .plist 中添加 NSLocationWhenInUseUsageDescription 这个键?如果您忘记了,请在 .plist 中添加此键和值 NSLocationWhenInUseUsageDescription = 应用程序想使用您的位置

标签: ios core-location cllocationmanager


【解决方案1】:

我认为, 您需要在项目中的.plist 中添加以下权限

NSLocationWhenInUseUsageDescription,该值为Application would like to use your location

第二个是,在您的代码中添加[locationManager requestWhenInUseAuthorization]; 行。

我添加了这段代码:

- (void)viewDidLoad {
    self.locationManager=[[CLLocationManager alloc]init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    [locationManager requestWhenInUseAuthorization];
    [locationManager startUpdatingLocation];

}

【讨论】:

    猜你喜欢
    • 2015-12-09
    • 2016-01-26
    • 2011-08-27
    • 2015-10-29
    • 2015-06-01
    • 2010-11-27
    • 1970-01-01
    • 2013-07-18
    • 2016-10-22
    相关资源
    最近更新 更多