先添加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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通