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


首先,需要导入 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. js权威指南学习笔记(三)语句

    1.声明语句 如果用var声明的变量没有初始化,那么这个变量的值会被初始化为undefined. 函数声明语句的语法如下:       4 4           1 console.log(func ...

  2. asp.net core 错误定位 & vs2017 远程调试部署在centos上的asp.net core程序

        前言 程序运行中会出现各种BUG. 排除BUG有三种方式. 一.访问页面直接报错误信息 出于安全,服务器是关闭这个功能的.在centos上可以用 命令设置环境变量来解决:   export A ...

  3. 日常捕获的野生知识 - javascript获取屏幕大小

    刚刚接触JavaScript,涉及到 document , window 的一些基本知识不是很了解,今天为了一个屏幕大小折腾了半天,幸好找到了很好的例子学习. 代码如下: <html> & ...

  4. MySQL基于mysqldump快速搭建从库

    MySQL主从搭建总的来说大致分为3个步骤: 1. 为主从实例添加复制所需参数以及创建复制用的账户 2. 需要 […]

  5. C# Winform窗体和控件自适应大小

    1.在项目中创建类AutoSizeForm AutoSizeForm.cs文件代码: using System; using System.Collections.Generic; using Sys ...

  6. CSS的BFC和hasLayout及其应用场景

    前端精选文摘:BFC 神奇背后的原理 一.BFC是什么? 先介绍 Box.Formatting Context的概念. Box: CSS布局的基本单位 Box 是 CSS 布局的对象和基本单位, 直观 ...

  7. QLocalServer和QLocalSocket单进程和进程通信

    QLocalServer 继承自QObject. QLocalServer提供了一个基于本地套接字(socket)的服务端(server).QLocalServer可以接受来自本地socket的连接. ...

  8. jdk1.8 对数组及arrays类对数组的操作与增强

    数组的初始化有两种方式 静态初始化: 初始化时由程序员显示置顶每个数组的初始值,由系统决定数组长度.如: int[] a1 = new int[] {1,2,3,4}; 动态初始化:初始化时由程序员只 ...

  9. springmvc/springboot处理前台字符串日期自动转换成后台date类型的三种办法

    参考https://blog.csdn.net/eumenides_/article/details/79033505 补充一个:Formatter也可以实现.

  10. asp.net ashx导出excel到前台

    最近有一个项目使用以前的ashx,不能使用FileResult,只有通过response返回拼接好的字符串.但是通过查阅资料拼接的字符串总是提示文件格式不匹配,虽然能正常打开,但是体验很不好,在此总结 ...