iOS常用隐私权限获取

2019-11-26  本文已影响0人  Geniune

从iOS10开始苹果更加注重用户隐私,很多例如:定位、相机、相册、蓝牙、通讯录等功能需要弹选择框经过用户同意后才能使用。如果用户点击不同意,但你仍坚持调用相关的函数,那App大多数时候就会华丽Crash!为了避免这些问题,可以通过一些方法获取到当前App对应用户隐私的权限状态。

先上代码

https://github.com/Geniune/LJPrefs

info.plist设置

首先要在info.plist中加入对应的key-value键值对,key如下图所示,value则需要输入你的App获取这个权限的目的,如果不设置运行会报错,例如:'NSHealthUpdateUsageDescription must be set in the app's Info.plist

Data Class Xcode Key
Apple Music Privacy - Media Library Usage Description
Bluetooth Privacy - Bluetooth Always Usage Description
Calendar Privacy - Calendars Usage Description
Camera Privacy - Camera Usage Description
Contacts Privacy - Contacts Usage Description
Health Privacy - Health Share Usage Description
Privacy - Health Update Usage Description
Home Privacy - HomeKit Usage Description
Location Privacy - Location When In Use Usage Description
Privacy - Location Always Usage Description
Privacy - Location Always and When In Use Usage Description
Microphone Privacy - Microphone Usage Description
Motion Privacy - Motion Usage Description
Photos Privacy - Photo Library Usage Description
Reminders Privacy - Reminders Usage Description
Siri Privacy - Siri Usage Description

权限代码判断

为了好理解我们用伪代码,以获取相册权限为例,拿到PHAuthorizationStatus枚举类型,可能有以下几种情况:

注意:每次调用UIImagePickerController之前都要判断status,若该权限被用户明确拒绝,不加判断App会有奔溃的风险!(在首次权限弹框点击”允许“后,可以到”设置“中手动将对应权限关闭,这里的判断主要就是为了预防此类问题)

如果本文对你有所帮助,记得点个赞哈

上一篇下一篇

猜你喜欢

热点阅读