关于介入地图相关功能后会遇到类似定位的子功能,由此引来了此定位权限授权相关.


首先,需要导入 CoreLocation 的框架并创建管理对象从而实现后续的相关操作;

#import <CoreLocation/CoreLocation.h>

其中里面会包含一些参数属性方法等,例如:
1)是否开启位置服务

/*
* locationServicesEnabled
*
* Discussion:
* Determines whether the user has location services enabled.
* If NO, and you proceed to call other CoreLocation API, user will be prompted with the warning
* dialog. You may want to check this property and use location services only when explicitly requested by the user.
*/
+ (BOOL)locationServicesEnabled API_AVAILABLE(ios(4.0), macos(10.7));

2)设置定位所期望的精准度,其中会有响应的枚举值可供选择,但精准度越高所消耗的设备电源性能也会随之受其影响,例如:导航模式

/*
* desiredAccuracy
*
* Discussion:
* The desired location accuracy. The location service will try its best to achieve
* your desired accuracy. However, it is not guaranteed. To optimize
* power performance, be sure to specify an appropriate accuracy for your usage scenario (eg,
* use a large accuracy value when only a coarse location is needed). Use kCLLocationAccuracyBest to
* achieve the best possible accuracy. Use kCLLocationAccuracyBestForNavigation for navigation.
* The default value varies by platform.
*/
@property(assign, nonatomic) CLLocationAccuracy desiredAccuracy; 精准度 desiredAccuracy 所对应的枚举值相关:
/*
* kCLLocationAccuracy<x>
*
* Discussion:
* Used to specify the accuracy level desired. The location service will try its best to achieve
* your desired accuracy. However, it is not guaranteed. To optimize
* power performance, be sure to specify an appropriate accuracy for your usage scenario (eg,
* use a large accuracy value when only a coarse location is needed).
*/
extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation API_AVAILABLE(ios(4.0), macos(10.7));// 最近
extern const CLLocationAccuracy kCLLocationAccuracyBest;// 最优
extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;// 十米
extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;// 百米
extern const CLLocationAccuracy kCLLocationAccuracyKilometer;// 千米
extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;// 三千米

3)设置定位距离过滤的参数,若此次与上次定位所产生的距离差值大于或等于此设定值时则会调用代理方法

/*
* distanceFilter
*
* Discussion:
* Specifies the minimum update distance in meters. Client will not be notified of movements of less
* than the stated value, unless the accuracy has improved. Pass in kCLDistanceFilterNone to be
* notified of all movements. By default, kCLDistanceFilterNone is used.
*/
@property(assign, nonatomic) CLLocationDistance distanceFilter;

4)开始 & 停止位置的更新

/*
* startUpdatingLocation
*
* Discussion:
* Start updating locations.
*/
- (void)startUpdatingLocation API_AVAILABLE(watchos(3.0)) API_UNAVAILABLE(tvos); /*
* stopUpdatingLocation
*
* Discussion:
* Stop updating locations.
*/
- (void)stopUpdatingLocation;

5)设置代理(delegate)后的一些常用代理方法

locationManager.delegate = self;

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
NSLog(@"获取定位信息 --- 成功");
} - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"获取定位信息 --- 失败");
}

其次,声明全局的变量 CLLocationManager,此处需要注意若使用局部变量的方式会调用授权方法失败;

/** 位置管理*/
CLLocationManager *locationManager;

再其次,初始化设置代理并配置系统位置权限授权操作相关
注:此处需要判断一下系统的版本号,避免异常闪退,因属性是在系统8.0基础之上才可以使用

#pragma mark - ****************************** 获取位置验证权限
/**
获取位置验证权限(作用域: 地图 & 定位相关)
@param vc 当前视图控件
*/
- (void)YHGetLocationPermissionVerifcationWithController:(UIViewController *)vc {
BOOL enable = [CLLocationManager locationServicesEnabled];
NSInteger state = [CLLocationManager authorizationStatus]; if (!enable || > state) {// 尚未授权位置权限
if ( <= [[UIDevice currentDevice].systemVersion floatValue]) {
NSLog(@"系统位置权限授权弹窗");
// 系统位置权限授权弹窗
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager requestAlwaysAuthorization];
[locationManager requestWhenInUseAuthorization];
}
}
else {
if (state == kCLAuthorizationStatusDenied) {// 授权位置权限被拒绝
NSLog(@"授权位置权限被拒绝");
UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"提示"
message:@"访问位置权限暂未授权"
preferredStyle:UIAlertControllerStyleAlert];
[alertCon addAction:[UIAlertAction actionWithTitle:@"暂不设置" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]]; [alertCon addAction:[UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
dispatch_after(0.2, dispatch_get_main_queue(), ^{
NSURL *url = [[NSURL alloc] initWithString:UIApplicationOpenSettingsURLString];// 跳转至系统定位授权
if( [[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
});
}]]; [vc presentViewController:alertCon animated:YES completion:^{ }];
}
}
}

GitHub: https://github.com/survivorsfyh/YHTools/tree/master/YHAccessAuthorization

______

以上便是此次内容小结,项目中用到的功能不多没有深挖,还有很多可拓展的地方,有什么不足还望多多指点!

												

定位权限授权 - iOS的更多相关文章

  1. 高德定位腾讯定位在APP上无法开启定位权限的解决方案

    [备注]公司项目中遇到的问题,如果你在团队工作其中定有不少配合方面的问题,其中的思路是可以借鉴的,因为这也许正是你们现在遇到的问题,总结的不好的地方还请多多指教 因为项目需求的确定,定位成了必不可少的 ...

  2. iOS 8以后 定位手动授权问题

    ios8以后 都是手动授权定位权限 不过不处理这块 在ios8以后的系统就会默认永不授权 即关闭了定位权限 处理办法如下 .导入框架头文件 #import <CoreLocation/CoreL ...

  3. iOS~判断应用是否有定位权限

    在特定场景下我们需要判断用户是否允许应用获取定位权限 1.导入类库: #import <CoreLocation/CLLocationManager.h> 2.判断用户手机是否开启了定位服 ...

  4. iOS定位权限请求时易犯的错误小结

    起因 用户群反馈app可能请求了不合适的定位权限:始终定位. 看到这个截图,根据经验判断可能是后台定位功能导致可能不得不请求始终定位权限.再加上之前提交审核时,苹果要求在plist文件中新增NSLoc ...

  5. 小程序调用wx.chooseLocation接口的时候无法获取权限(ios)

    ios手机小程序调用wx.chooseLocation接口的时候,获取权限的时候报authorize:fail:require permission desc这样子的错误,这是由于苹果的安全机制导致需 ...

  6. 解决在iOS8环境下,当用户关闭定位服务总开关时,无法将APP定位子选项加入定位权限列表的问题

    关键点:- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizati ...

  7. ArcGIS Runtime SDK for Android 定位权限(GPS定位\网络定位)

    ACCESS_COARSE_LOCATION和ACCESS_FINE_LOCATION: android.permission.ACCESS_COARSE_LOCATION:是基站定位,即基于无线网络 ...

  8. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(24)-权限管理系统-将权限授权给角色

    系列目录 过了个年回来,回顾一下,我们上次讲了角色管理,我们这一次来讲将权限授权给角色,这一节也是大家比较关心的.因为我们已经跑通了整个系统,知道权限的流转,我们先来看一张图 这张图主要分要3块,角色 ...

  9. Android填坑系列:在小米系列等机型上放开定位权限后的定位请求弹框

    背景: 近期因实际项目需要,在特定操作下触发定位请求,取到用户位置及附近位置. 问题: 经初步选型,最终决定接入百度定位,按照百度定位SDK Android文档,接入过程相对顺利.但随后发现,在小米系 ...

随机推荐

  1. MySQL查询(未完结)

    MySql查询 单表查询: 查询所有字段 SELECT * FROM 表名; '*' 代表所有字段 查询指定字段 SELECT 字段名1, 字段名2 FROM 表名; 按照指定条件查询记录 1. 查询 ...

  2. centos yum安装常用命令

    安装killall命令 yum install -y psmisc 安装sz(下载)和rz(上传)命令 yum install -y lrzsz 安装 ifconfig 命令 yum install ...

  3. apply()方法和call()方法

    obj.func.call(obj1)       //是将obj1看做obj,调用func方法,将第一个参数看做函数调用的对象,可以看做,将obj的方法给obj1使用 ECMAScript规范给所有 ...

  4. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.8:unpack (unpack) on project sq-integral-web: Unable to find artifact.

    1.问题描述 项目maven打包报上述错误, 但是小伙伴运行好使. 2.问题解决 是idea工程编码(gbk)和项目编码(utf-8)不一致 idea->file->Other Setti ...

  5. Unicode汉字转码小工具

    点击按钮即可使用! 在这里粘贴或输入       转换结果:

  6. 【Python】猜数小游戏(文件操作)

    人生苦短,我用Python 关键词 1.多用户 2.字典记录所有成绩 3.每次游戏轮数&总游戏次数&平均每次游戏需要多少轮 字典Dictionary.列表List.元组Tuple差异化 ...

  7. 动态注册broadcast的安全考虑

    一.android service通知activity更新方式有1. service 通过广播的形式发送broadcast,向这个activity的内部类发广播的消息来更新界面2. service直接 ...

  8. Studying TCP's Congestion Window using NS

    Studying TCP's Congestion Window using NS How to obtain TCP's CWND value The most important value th ...

  9. windows系统的错误码

    https://blog.csdn.net/u011785544/article/details/51682290

  10. AutoHotkey的函数对象的Bind方法绑定参数的应用

    近期在写Excel数据批处理函数,想提取某列的每个数据是否匹配某某条件的所有单元格. 这种需求比较多,比如判断的值有:单元格值,字体颜色,单元格颜色等等, 判断条件有:相同,不同,正则,或在某多行字符 ...