iOS14:定位的授权配置总结

一、概述

Apple特别注重用户隐私的保护,位置定位信息更是用户隐私重要一环。Apple针对Location的获取有严格的规定和流程,开发中需要严格按照流程来,否则或获取不到定位信息或者无法上架。

二、基础知识

iOS关于定位的framework是CLLocationManager,相关的api均在这里面。

1、系统的全局定位开关

[CLLocationManager locationServicesEnabled];

2、定位的状态标识

CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
typedef NS_ENUM(int, CLAuthorizationStatus) {
      kCLAuthorizationStatusNotDetermined = 0,  // 用户未授权,即还未弹出OS的授权弹窗
      kCLAuthorizationStatusDenied, // 用户拒绝定位权限,包括拒绝App或者全局开关关闭
      kCLAuthorizationStatusRestricted, // 定位服务受限,该状态位用户无法通过设置页面进行改变
      kCLAuthorizationStatusAuthorizedAlways, // 始终定位,即后台定位
      kCLAuthorizationStatusAuthorizedWhenInUse, // App使用的时候,允许定位
      kCLAuthorizationStatusAuthorized, // iOS8.0之后已经被废弃
};

定位的状态变化,会通过@property(weak, nonatomic, nullable) id delegate;进行回调。

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {

}

三、始终定位Always 和 App使用期间定位WhenInUse 两种定位模式配置

两种模式的都需要初始化CLLocationManagerd的实例self.locationManager = [[CLLocationManager alloc] init];,然后调用实例方法。

3.1、“使用期间定位”模式

a、info.plist 配置 NSLocationWhenInUseUsageDescription

b、调用方法requestWhenInUseAuthorization申请使用期间定位模式
有且只有status == kCLAuthorizationStatusNotDetermined的时候,调用才会出现系统弹窗。

注意:如果用户选择“允许一次”,则状态更改为kCLAuthorizationStatusAuthorizedWhenInUse,但是设置还是为"询问"状态,下次App启动的时候,还是status == kCLAuthorizationStatusNotDetermined需要进行授权弹窗。

c、之后如果还需要定位,则需要自己弹窗提醒用户

3.2、“始终定位”模式

只有当你的App确实需要始终定位的时候,才配置。该模式下,AppStore的审核也会更加的严格。
a、info.plist 同时配置以下项目
NSLocationAlwaysAndWhenInUseUsageDescription
NSLocationWhenInUseUsageDescription
需要支持 iOS10 的话需要配置 NSLocationAlawaysUsageDescription

b、调用方法requestAlwaysAuthorization
该方法的调用时机非常重要,否则可能永远都出不来弹窗。

场景一、
status == kCLAuthorizationStatusNotDetermined时,必须首先调用requestWhenInUseAuthorization,只有用户同意“应用内使用”的情况下才有用。

status == kCLAuthorizationStatusAuthorizedWhenInUse时,调用requestAlwaysAuthorization出现始终授权模式弹窗。

场景二、试过好像没有达到条件,需要继续摸索。
status == kCLAuthorizationStatusNotDetermined时,直接调用requestWhenInUseAuthorization,官方文档说会出现两次弹窗。第一次弹窗征求用户同意使用期间授权模式,同意后,如果App短时处于使用定位状态,将会出现二次弹窗。 If the app is in the Provisional Always state, the system displays the second prompt with the string from NSLocationAlwaysUsageDescription.

c、之后如果还需要定位,则需要自己弹窗提醒用户

requestAlwaysAuthorization 方法的官方文档

四、模糊定位,iOS14适配

  • 4.1、模糊定位状态
self.locationManager = [[CLLocationManager alloc] init];
CLAccuracyAuthorization status = self.locationManager.accuracyAuthorization
typedef NS_ENUM(NSInteger, CLAccuracyAuthorization) {
    CLAccuracyAuthorizationFullAccuracy, //精准定位
    CLAccuracyAuthorizationReducedAccuracy, // 模糊定位
};
  • 4.2、可以通过直接在 info.plist 中添加 NSLocationDefaultAccuracyReduced 为 true 默认请求大概位置。
    这样设置之后,即使用户想要为该 App 开启精确定位权限,也无法开启。

  • 4.3、可以直接通过API来根据不同的需求设置不同的定位精确度。

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyReduced;
  • 4.4、临时一次精准定位弹窗 —— 每次调用否会弹窗,iOS无限制
    在 Info.plist 中配置NSLocationTemporaryUsageDescriptionDictionary字典中需要配置 key 和 value 表明使用位置的原因,以及具体的描述。 key为自定义的字段,在接口中传入PurposeKey。
    然后调用方法[self.mgr requestTemporaryFullAccuracyAuthorizationWithPurposeKey:@"purposeKey"];

淘系技术-iOS14 隐私适配及部分解决方案

你可能感兴趣的:(iOS14:定位的授权配置总结)