2016-04-04 64 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]; 

} 
相關問題