標籤:
在IOS8更新以後以前的方法CLLocationManagerDelegate不調用didUpdateLocations
iOS8修改了位置設定裡的內容,增加了一套狀態(使用中可用/通常可用),所以以前的CLLcationManage的註冊後,
Delegate介面不響應了,研究了一上午終於可以用了
說一下我的心得
(1)添加corelocation.framework
(2)在Info.plist中加入兩個預設沒有的欄位
(3)匯入標頭檔
#import <CoreLocation/CoreLocation.h>
#import <CoreLocation/CLLocationManagerDelegate.h>
@interface PersonViewController : UIViewController<CLLocationManagerDelegate>{ CLLocationManager* locationManager;}@property (strong, nonatomic) CLLocationManager* locationManager;@end
(4)執行個體化並完成代理方法
-(void)startLocation{ [self.locationManager requestAlwaysAuthorization]; if ([CLLocationManager locationServicesEnabled]&&[CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied) { self.locationManager = [[CLLocationManager alloc] init]; [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; self.locationManager.delegate = self; if (__IPHONE_8_0) { NSLog(@"here"); self.locationManager.distanceFilter = kCLDistanceFilterNone; [self.locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; } NSLog(@"that‘s here"); [self.locationManager startUpdatingLocation]; }}- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { switch (status) { case kCLAuthorizationStatusNotDetermined: if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { [locationManager requestWhenInUseAuthorization]; } break; default: break; }}//定位代理經緯度回調-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ [locationManager stopUpdatingLocation]; NSLog(@"location ok"); CLLocation *currentLocation = [locations firstObject]; NSLog(@"%@",[NSString stringWithFormat:@"經度:%3.5f\n緯度:%3.5f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude]); CLGeocoder * geoCoder = [[CLGeocoder alloc] init]; [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { for (CLPlacemark * placemark in placemarks) { NSDictionary *test = [placemark addressDictionary]; // Country(國家) State(城市) SubLocality(區) NSLog(@"%@", [test objectForKey:@"State"]); _locationCity.text = [test objectForKey:@"SubLocality"]; } }];}- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { if (error.code == kCLErrorDenied) { NSLog(@"location%ld",(long)error.code); }}
(5)設定開始定位結束定位
- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self startLocation]; // 開始定位}- (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [locationManager stopUpdatingLocation]; // 停止定位}
如果還不能定位的歡迎留言交流!
iOS8更新後定位問題CLLcationManage