前言:刚开始入门iOS时,看了几天的理论知识后,就开始着手写一个简单的天气预报程序练手。过程中就遇到了首页需要根据定位出的城市获取天气的问题,于是搜索了一下获取定位相关的知识,整理如下.
根据个人学习是的习惯,觉得网上大神的解答最好能有个简单的demo可供参考最好,所以本人写了个简单的获取定位的demo:
标注一下demo源码位置iOS Objective-C获取定位城市demo
导入头文件并请求授权
导入头文件
1修改info.plist
修改info.plist文件,添加获取定位行为以及配置选项。如下图:
可以只添加需要时使用:Privacy - Location When In Use Usage Description
创建位置管理者CLLocationManager,并添加到属性
|
|
判断是否开启了定位服务
在程序启动时需要先判断是否开启了定位
1234if ([CLLocationManager locationServicesEnabled]) { // 判断是否打开了位置服务self.locationManager = [[CLLocationManager alloc] init];//初始化定位器[self.locationManager startUpdatingLocation]; // 启动定位器}设置定位器的其他属性
在初始化定位器后,需要设置其delegate以及其他属性123456789if ([CLLocationManager locationServicesEnabled]){self.locationManager = [[CLLocationManager alloc] init];//初始化定位器self.locationManager.delegate = self; //设置代理self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;//设置精确度self.locationManager.distanceFilter = 100;//表明每隔100米更新一次定位信息[self.locationManager requestWhenInUseAuthorization];//使用的是requestWhenInUseAuthorization方法self.currentCity = [[NSString alloc] init];[self.locationManager startUpdatingLocation];//启动定位器}
监听定位信息
定位成功时调用
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{ [self.locationManager stopUpdatingLocation]; CLLocation *currentLocation = [locations lastObject]; CLGeocoder *geoCoder = [[CLGeocoder alloc] init]; //反编码 [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error){ if (placemarks.count > 0){ CLPlacemark *placeMark = placemarks[0]; self.currentCity = placeMark.locality; if (!self.currentCity){ self.currentCity = @"无法定位当前城市"; } self.cityLable.text = self.currentCity; NSLog(@"%@",self.currentCity); } else if (error == nil&& placemarks.count ==0){ NSLog(@"No location and error return"); } else if (error){ NSLog(@"location error: %@ ",error); } }]; }
定位失败时调用
demo中定位失败只是简单的弹起alert,具体的alert内容下面会介绍//定位失败的异常处理 - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { [self showLocationAlert]; }
定位异常处理
这里所谓的处理异常是在定位未开启时,弹出alert并引导去打开定位的操作,所以需要在设置定位器的[CLLocationManager locationServicesEnabled]
返回nil时调用[self showLocationAlert];
具体alert中的内容如下://定位失败弹出提示框 -(void)showLocationAlert{ UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"允许\"定位\"提示" message:@"请在设置中打开定位" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction * ok = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //打开定位设置 NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; [[UIApplication sharedApplication] openURL:settingsURL]; }]; UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alertVC addAction:cancel]; [alertVC addAction:ok]; [self presentViewController:alertVC animated:YES completion:nil]; }
demo源码位置再次贴出
iOS Objective-C获取定位城市demo