先添加Corelocation.framework框架
在info.plist中添加
Privacy - Location When In Use Usage Description string -需要你的同意 才能访问位置
在APPdelegate.m中 这里 启动的时候提示一次
#import <CoreLocation/CoreLocation.h>
添加代理CLLocationManagerDelegate
@property(nonatomic,strong)CLLocationManager *locationManager;
if ([CLLocationManager locationServicesEnabled]) {
_locationManager = [[CLLocationManager alloc]init];
_locationManager.delegate = self;
[_locationManager requestAlwaysAuthorization];
[_locationManager requestWhenInUseAuthorization];
//设置寻址精度
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = 5.0;
[_locationManager startUpdatingLocation];
}
如果没有开启定位
在需要定位的地方
#import <CoreLocation/CoreLocation.h>
添加代理CLLocationManagerDelegate
@property(nonatomic,strong)CLLocationManager *locationManager;//定位
if ([CLLocationManager locationServicesEnabled]) {
_locationManager = [[CLLocationManager alloc]init];
_locationManager.delegate = self;
[_locationManager requestAlwaysAuthorization];
[_locationManager requestWhenInUseAuthorization];
//设置寻址精度
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = 5.0;
[_locationManager startUpdatingLocation];
}
#pragma mark ------定位失败
/*地位失败则执行此代理方法*/
/*定位失败弹出提示窗,点击打开定位按钮 按钮,会打开系统设置,提示打开定位服务*/
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
/*设置提示提示用户打开定位服务*/
UIAlertController * alert = [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 * cacel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:ok];
[alert addAction:cacel];
[self presentViewController:alert animated:YES completion:nil];
}
/*定位成功后则执行此代理方法*/
#pragma mark 定位成功
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
[_locationManager stopUpdatingLocation];
/*旧值*/
CLLocation * currentLocation = [locations lastObject];
// CLGeocoder * geoCoder = [[CLGeocoder alloc]init];
/*打印当前经纬度*/
NSLog(@"%f%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
latitudeStr = [NSString stringWithFormat:@"%f",currentLocation.coordinate.latitude];
longitudeStr = [NSString stringWithFormat:@"%f",currentLocation.coordinate.longitude];
}
在需要的地方写下
if (latitudeStr == nil) {
/*设置提示提示用户打开定位服务*/
UIAlertController * alert = [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 * cacel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:ok];
[alert addAction:cacel];
[self presentViewController:alert animated:YES completion:nil];
return;
}