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


首先,需要导入 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. 洛谷P1024 一元三次方程求解(数学)

    题意 题目链接 Sol 本来是一道好的公式题. 然后输出只要保留两位小数?? 直接上不就赢了嘛.. #include<bits/stdc++.h> #define LL long long ...

  2. 【Android】13.0 UI开发(四)——列表控件RecyclerView的横向布局排列实现

    1.0 新建项目,由于ListView的局限性,RecyclerView是一种很好取代ListView的控件,可以灵活实现多种布局. 2.0 新建项目RecyclerviewTest,目录如下: 3. ...

  3. Python代码 变量None的使用

    代码中经常会有变量是否为None的判断,有三种主要的写法: 第一种是'if x is None': 第二种是 'if not x:': 第三种是'if not x is None'(这句这样理解更清晰 ...

  4. 基于容器微服务的PaaS云平台设计(一) 实现容器微服务和持续集成

    版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://www.cnblogs.com/SuperXJ/ 前言:关于什么是容器微服务PaaS和容器微服务PaaS的 ...

  5. python shopping incomplete code

    #shopping code#shopping.py#导入登录模块import login# shop car beginningsalary = input("请输入工资:\t" ...

  6. mongodb数据库集合操作

    1:更新update update() 方法用于更新已存在的文档.语法格式如下: db.collection.update( <query>, <update>, { upse ...

  7. svn merge error must be ancestrally related to,trunk merge branch报错

    trunk merge branch的时候报错 xxx must be ancestrally related to xxx,这个报错的意思是两者不关联,所以需要去建立关联. [回顾背景]       ...

  8. Oracle实例初始化参数详解

    BACKGROUND_DUMP_DEST 显示和设置Oracle数据库相关日志的存放地,Oracle11g后不再可配置,但其值仍可显示相关日志的存放地,对应配置参数为diagnostic_dest U ...

  9. Linux中nmon的安装与使用

    一.下载nmon. 根据CPU的类型选择下载相应的版本:http://nmon.sourceforge.net/pmwiki.php?n=Site.Downloadwget http://source ...

  10. Deep Learning Drizzle

    Deep Learning Drizzle Drench yourself in Deep Learning, Reinforcement Learning, Machine Learning, Co ...