IOS-didFailWithError : Error Domain=kCLErrorDomain Code=0.(kCLErrorDomain 错误 0。)”

标签 ios core-location cllocationmanager

<分区>

我尝试获取用户当前位置的经度和纬度,我的代码很简单:

- (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.)" 

enter image description here

最佳答案

我觉得 您需要在项目的.plist 中添加以下权限

NSLocationWhenInUseUsageDescription 并且该值为Application would like to use your location

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

this code in i added :

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

}

关于IOS-didFailWithError : Error Domain=kCLErrorDomain Code=0.(kCLErrorDomain 错误 0。)”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36399169/

上一篇:iOS 应用程序 : Enabling logging with minimal changes to the code

下一篇:ios - 所有 View 的监听器

相关文章:

ios - 为什么基于 Firebase 的应用程序在伊朗无法运行?

Swift获取变量中的坐标纬度经度

ios - 在iOS中获取iBeacon的地理位置经度和纬度

ios8 - iOS 8 不请求位置权限

ios - 为什么 locationManager didUpdateLocation 不工作?

ios - 框架原点与一组不同

ios - Xcode 的 Profiler 不显示符号名称

ios - 如何在ios中的scrollview中添加文本字段?

ios - 核心定位初始化和委托(delegate)

iphone - GPS 位置在我的应用程序的 iphone 中打开/关闭