2016-04-04 67 views
0

我尝试获取用户当前位置的经度纬度&,我的代码很简单:IOS-didFailWithError:错误域= kCLErrorDomain代码= 0。 (kCLErrorDomain误差为0)”

- (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

+0

我复位网络,但仍然没有工作。 @derpoliuk –

+0

@ Mr.Bond请在模拟器中检查您的位置调试>位置>当前位置 –

+0

这也不起作用。@ bhavin ramani –

回答

1

我认为, 你需要在你的项目中添加下列权限在.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]; 

} 
相关问题