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

首先,需要导入 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的更多相关文章
- 高德定位腾讯定位在APP上无法开启定位权限的解决方案
[备注]公司项目中遇到的问题,如果你在团队工作其中定有不少配合方面的问题,其中的思路是可以借鉴的,因为这也许正是你们现在遇到的问题,总结的不好的地方还请多多指教 因为项目需求的确定,定位成了必不可少的 ...
- iOS 8以后 定位手动授权问题
ios8以后 都是手动授权定位权限 不过不处理这块 在ios8以后的系统就会默认永不授权 即关闭了定位权限 处理办法如下 .导入框架头文件 #import <CoreLocation/CoreL ...
- iOS~判断应用是否有定位权限
在特定场景下我们需要判断用户是否允许应用获取定位权限 1.导入类库: #import <CoreLocation/CLLocationManager.h> 2.判断用户手机是否开启了定位服 ...
- iOS定位权限请求时易犯的错误小结
起因 用户群反馈app可能请求了不合适的定位权限:始终定位. 看到这个截图,根据经验判断可能是后台定位功能导致可能不得不请求始终定位权限.再加上之前提交审核时,苹果要求在plist文件中新增NSLoc ...
- 小程序调用wx.chooseLocation接口的时候无法获取权限(ios)
ios手机小程序调用wx.chooseLocation接口的时候,获取权限的时候报authorize:fail:require permission desc这样子的错误,这是由于苹果的安全机制导致需 ...
- 解决在iOS8环境下,当用户关闭定位服务总开关时,无法将APP定位子选项加入定位权限列表的问题
关键点:- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizati ...
- ArcGIS Runtime SDK for Android 定位权限(GPS定位\网络定位)
ACCESS_COARSE_LOCATION和ACCESS_FINE_LOCATION: android.permission.ACCESS_COARSE_LOCATION:是基站定位,即基于无线网络 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(24)-权限管理系统-将权限授权给角色
系列目录 过了个年回来,回顾一下,我们上次讲了角色管理,我们这一次来讲将权限授权给角色,这一节也是大家比较关心的.因为我们已经跑通了整个系统,知道权限的流转,我们先来看一张图 这张图主要分要3块,角色 ...
- Android填坑系列:在小米系列等机型上放开定位权限后的定位请求弹框
背景: 近期因实际项目需要,在特定操作下触发定位请求,取到用户位置及附近位置. 问题: 经初步选型,最终决定接入百度定位,按照百度定位SDK Android文档,接入过程相对顺利.但随后发现,在小米系 ...
随机推荐
- cf1043F. Make It One(dp 容斥原理)
题意 题目链接 给出\(n\)个数,问最少选几个数,使他们的\(gcd = 1\) Sol 好神仙啊qwq. 首先,如果答案存在,那么最多为\(7\)(因为前\(7\)个质数乘起来\(>= 3e ...
- easyui numberbox 输入框禁止输入
{ field: 'Amount', title: '金额', width: 80, editor: { type: 'numberbox', options: { disabled: true, p ...
- jQuery阻止向上冒泡事件
//阻止起泡取消下面的注释 e.stopPropagation(); //或者使用这种方式 //return false; }); $('.three').click(function(e){ ale ...
- sqlite 时间函数及时间处理
SQLite分页显示:Select * From news order by id desc Limit 10 Offset 10这篇文章是根据 SQLite 官方 WIKI 里的内容翻译,如果有什么 ...
- double转换long的疑问
在lua(5.1.4)下面测试的时候使用0x100000000的时候出现了问题,打印结果很明显,如下所示: Lua Copyright (C) - Lua.org, PUC-Rio > prin ...
- PHP-Gealman
一.简介 Gearman是一个分发任务的程序框架,它会对作业进行排队自动分配到一系列机器上.gearman跨语言跨平台,很方便的实现异步后台任务.php官方收录:http://php.net/manu ...
- Hadoop学习---Eclipse中hadoop环境的搭建
在eclipse中建立hadoop环境的支持 1.需要下载安装eclipse 2.需要hadoop-eclipse-plugin-2.6.0.jar插件,插件的终极解决方案是https://githu ...
- Toad for MySQL 7.3 Freeware异常 2017-01-09 15:14 115人阅读 评论(0) 收藏
打开Toad出现如下异常信息: 解决办法: 重装.NET Framework4.0
- 贴现率d与利率i
一.复利中的实际利率 it=(1+i)t -(1+i)t-1 / (1+i)t-1=i i 为常数, 而单利的实际利率递减 二.贴现 时间t的1元在时间零点的价值为贴现函数 记为 a-1(t) ...
- OC NSMutableString的使用
#pragma mark 可变字符串的创建 void stringCreate() { // 预先分配10个字数的存储空间 NSMutableString *str = [[NSMutableStri ...