環(huán)境:XCODE6.4 + iPhone / iOS8
一 錯誤:使用CoreLocation獲取地理位置信息掸哑,報錯
Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)"
解決方法:
1.確定模擬器(手機(jī))已經(jīng)聯(lián)網(wǎng)并且允許程序獲取地理位置
2.模擬器位置已設(shè)置(可以設(shè)置自定義)
2.重置地理位置服務(wù)或者網(wǎng)絡(luò)服務(wù)
PS:如果是模擬器就果斷直接重置模擬器吧 ?IOS Simulator - Reset Content and Settings..
二 ios8.0下的定位服務(wù)需要申請授權(quán).具體代碼如下:
if ([CLLocationManager locationServicesEnabled]) {
self.locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest; //控制定位精度,越高耗電量越大。
_locationManager.distanceFilter = 100; //控制定位服務(wù)更新頻率翎迁。單位是“米”
[_locationManager startUpdatingLocation];
//在ios 8.0下要授權(quán)
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[_locationManager requestWhenInUseAuthorization];? //調(diào)用了這句,就會彈出允許框了.
}
注意:
在Info.plist文件還要加上
NSLocationWhenInUseUsageDescription ? 描述巡蘸、、為什么使用定位
NSLocationAlwaysUsageDescription ? ? ? ? string “提示描述”
#pragma mark - CLLocationManagerDelegate
//定位到用戶位置時調(diào)用定位比較頻繁
- (void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)locations{
CLLocation*currLocation = [locationslastObject];
NSLog(@"經(jīng)度=%f緯度=%f高度=%f", currLocation.coordinate.latitude, currLocation.coordinate.longitude, currLocation.altitude);
NSLog(@"locationManager---%ld",locations.count);
}
- (void)locationManager:(CLLocationManager*)manager didFailWithError:(NSError*)error
{
if([errorcode] ==kCLErrorDenied)
{
//訪問被拒絕
NSLog(@"訪問被拒絕");
}
if([errorcode] ==kCLErrorLocationUnknown) {
//無法獲取位置信息
NSLog(@"無法獲取位置信息---%@",error);
}
}