iOS8更新后定位问题CLLcationManage

在IOS8更新以后以前的方法CLLocationManagerDelegate不调用didUpdateLocations

iOS8修改了位置设置里的内容,增加了一套状态(使用中可用/通常可用),所以以前的CLLcationManage的注册后, 
Delegate接口不响应了,研究了一上午终于可以用了

说一下我的心得

(1)添加corelocation.framework

(2)在Info.plist中加入两个缺省没有的字段

  • NSLocationAlwaysUsageDescription

  • NSLocationWhenInUseUsageDescription

(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
纬度:%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]; // 停止定位
}

如果还不能定位的欢迎留言交流!

原文地址:https://www.cnblogs.com/csdnIOS/p/4203057.html