作者:金田
iOS系统版本的不断升级的前提,伴随着用户使用设备的安全性提升,iOS系统对于App需要使用的硬件限制也越来越严格,App处理稍有不妥,轻则造成功能不可用用户还不知道,重则会造成App Crash。
当用户在App启动时,看到弹出来的一条条“XXX 请求访问您的位置” “XXX 请求访问您的通讯录” “XXX 请求访问您的日历” “XXX 请求访问您的摄像头” 等一系列消息时,用户觉得不耐烦的同时,也会由于一时的安全考虑而把相应的功能给屏蔽掉,这还只是开始,当用户真正在使用对应功能的时候,就会出现一连续的奇怪现象,比如数据显示异常:明明通讯录里面有信息,却总是加载不出数据;有的甚至是直接Crash。
下面,笔者将会综合性地把上述硬件的授权检测,一一地详细列出,并给出相关示例代码:
1、定位服务
相关框架
1
|
#import |
检测方法
1
|
+ (CLAuthorizationStatus)authorizationStatus |
相关返回参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
//用户尚未做出选择 kCLAuthorizationStatusNotDetermined = 0, // 未授权,且用户无法更新,如家长控制情况下 kCLAuthorizationStatusRestricted, // 用户拒绝App使用 kCLAuthorizationStatusDenied, // 总是使用 kCLAuthorizationStatusAuthorizedAlways NS_ENUM_AVAILABLE(NA, 8_0), // 按需使用 kCLAuthorizationStatusAuthorizedWhenInUse NS_ENUM_AVAILABLE(NA, 8_0), // 已授权 kCLAuthorizationStatusAuthorized |
参考代码
1
2
3
4
5
6
7
8
9
10
11
12
|
__block void (^checkLocationAuth)(void) = ^{ CLAuthorizationStatus authStatus = [CLLocationManager authorizationStatus]; if (CLAuthorizationStatusAuthorized == authStatus) { //授权成功,执行后续操作 } else { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ checkLocationAuth(); }); } }; checkLocationAuth(); |
2、通讯录
1
|
#import |
检测方法
1
|
ABAuthorizationStatus ABAddressBookGetAuthorizationStatus(void) |
授权状态
1
2
3
4
|
kABAuthorizationStatusNotDetermined = 0, // 未进行授权选择 kABAuthorizationStatusRestricted, // 未授权,且用户无法更新,如家长控制情况下 kABAuthorizationStatusDenied, // 用户拒绝App使用 kABAuthorizationStatusAuthorized // 已授权,可使用 |
参考代码
1
2
3
4
5
6
7
8
9
10
11
12
|
__block void (^checkAddressBookAuth)(void) = ^{ ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus(); if (kABAuthorizationStatusAuthorized == authStatus) { //授权成功,执行后续操作 } else { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ checkAddressBookAuth(); }); } }; checkAddressBookAuth(); |
3、日历/提醒事项授权
1
|
#import |
检测方法
1
|
+ (EKAuthorizationStatus)authorizationStatusForEntityType:(EKEntityType)entityType |
参数类型
1
2
3
|
EKEntityTypeEvent, //事件 EKEntityTypeReminder //提醒 |
授权状态
1
2
3
4
5
6
7
|
EKAuthorizationStatusNotDetermined = 0, // 未进行授权选择 EKAuthorizationStatusRestricted, // 未授权,且用户无法更新,如家长控制情况下 EKAuthorizationStatusDenied, // 用户拒绝App使用 EKAuthorizationStatusAuthorized, // 已授权,可使用 |
参考代码
1
2
3
4
5
6
7
8
9
10
11
12
|
__block void (^checkCalanderAuth)(void) = ^{ EKAuthorizationStatus authStatus = [EKAuthorizationStatus authorizationStatusForEntityType:EKEntityTypeEvent]; if (EKAuthorizationStatusAuthorized == authStatus) { //授权成功,执行后续操作 } else { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ checkCalanderAuth(); }); } }; checkCalanderAuth(); |
4、照片库授权
1
|
#import |
检测方法
1
|
+ (ALAuthorizationStatus)authorizationStatus; |
授权状态
1
2
3
4
5
6
7
|
ALAuthorizationStatusNotDetermined = 0, // 未进行授权选择 ALAuthorizationStatusRestricted, // 未授权,且用户无法更新,如家长控制情况下 ALAuthorizationStatusDenied, // 用户拒绝App使用 ALAuthorizationStatusAuthorized, // 已授权,可使用 |
参考代码
1
2
3
4
5
6
7
8
9
10
11
12
|
__block void (^checkAssetLibraryAuth)(void) = ^{ ALAuthorizationStatus authStatus = [ALAuthorizationStatus authorizationStatus]; if (ALAuthorizationStatusAuthorized == authStatus) { //授权成功,执行后续操作 } else { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ checkAssetLibraryAuth(); }); } }; checkAssetLibraryAuth(); |
5、蓝牙授权状态检测
1
|
#import |
检测方法
1
|
+ (CBPeripheralManagerAuthorizationStatus)authorizationStatus; |
授权状态
1
2
3
4
5
6
7
|
CBPeripheralManagerAuthorizationStatusNotDetermined = 0, // 未进行授权选择 CBPeripheralManagerAuthorizationStatusRestricted, // 未授权,且用户无法更新,如家长控制情况下 CBPeripheralManagerAuthorizationStatusDenied, // 用户拒绝App使用 CBPeripheralManagerAuthorizationStatusAuthorized, // 已授权,可使用 |
参考代码
1
2
3
4
5
6
7
8
9
10
11
12
|
__block void (^checkPeripheralAuth)(void) = ^{ CBPeripheralManagerAuthorizationStatus authStatus = [CBPeripheralManagerAuthorizationStatus authorizationStatus]; if (CBPeripheralManagerAuthorizationStatusAuthorized == authStatus) { //授权成功,执行后续操作 } else { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ checkPeripheralAuth(); }); } }; checkPeripheralAuth(); |
6、摄像头授权状态检测
1
|
#import |
检测方法
1
|
+ (AVAuthorizationStatus)authorizationStatusForMediaType:(NSString *)mediaType; |
授权状态
1
2
3
4
5
6
7
|
AVAuthorizationStatusNotDetermined = 0, // 未进行授权选择 AVAuthorizationStatusRestricted, // 未授权,且用户无法更新,如家长控制情况下 AVAuthorizationStatusDenied, // 用户拒绝App使用 AVAuthorizationStatusAuthorized, // 已授权,可使用 |
参考代码
1
2
3
4
5
6
7
8
9
10
11
12
|
__block void (^checkCameraAuth)(void) = ^{ AVAuthorizationStatus authStatus = [AVAuthorizationStatus authorizationStatusForMediaType:AVMediaTypeVideo]; if (AVAuthorizationStatusAuthorized == authStatus) { //授权成功,执行后续操作 } else { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ checkCameraAuth(); }); } }; checkCameraAuth(); |
7、麦克风授权状态检测
1
|
#import |
检测方法
1
|
- (AVAudioSessionRecordPermission)recordPermission; |
授权状态
1
2
3
4
5
|
AVAudioSessionRecordPermissionUndetermined, // 用户尚未被请求许可。 AVAudioSessionRecordPermissionDenied, // 用户已被要求并已拒绝许可。 AVAudioSessionRecordPermissionGranted, // 用户已被要求并已授予权限。 |
参考代码
1
2
3
4
5
6
7
8
9
10
11
12
|
__block void (^checkRecordPermission)(void) = ^{ AVAudioSessionRecordPermission authStatus = [[AVAudioSession sharedInstance] recordPermission]; if (AVAudioSessionRecordPermissionGranted == authStatus) { //授权成功,执行后续操作 } else { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ checkRecordPermission(); }); } }; checkRecordPermission(); |
------------------------------------------------------------------------------------------------------------------------------------
整理下iOS开发中常用的权限控制,只整理里一些常用的并不全。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#import typedef void (^AuthorizedFinishBlock)(); @interface LYAuthorizedMaster : NSObject #pragma mark - 摄像头权限 +( BOOL )checkCameraAuthority; +( void )cameraAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail; #pragma mark - 麦克风权限 +( BOOL )checkAudioAuthority; +( void )audioAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail; #pragma mark - 相册权限 +( BOOL )checkAlbumAuthority; +( void )albumAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail; #pragma mark - 推送通知权限 +( BOOL )checkPushNotificationAuthority; +( void )pushNotificationAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail; #pragma mark - 推送通知权限 +( BOOL )checkLocationAuthority; +( void )locationAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail; #pragma mark - 通讯录权限 +( BOOL )checkAddressBookAuthority; +( void )AddressBookAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail; |
下面是.m文件
里面引入了很多库文件,也不是所用项目都会用到的,用不到的注掉就好。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
#import "LYAuthorizedMaster.h" #import //摄像头麦克风 必须 #import //相册权限 #import //位置权限 #import //通讯录权限 #import "AppDelegate.h" #define kAPPName [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"] @implementation LYAuthorizedMaster #pragma mark - +( BOOL )checkAuthority:(AVAuthorizationStatus)_status{ return (_status == AVAuthorizationStatusAuthorized) || (_status == AVAuthorizationStatusNotDetermined); } +( void )showAlertController:(AuthorizedFinishBlock)_block device:(NSString *)_device{ UIAlertController *_alertC = [UIAlertController alertControllerWithTitle:@ "没有权限" message:[NSString stringWithFormat:@ "请开启‘%@’对 %@ 的使用权限" ,kAPPName,_device] preferredStyle:UIAlertControllerStyleAlert]; [_alertC addAction:[UIAlertAction actionWithTitle:@ "取消" style:UIAlertActionStyleCancel handler:nil]]; [_alertC addAction:[UIAlertAction actionWithTitle:@ "确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; }]]; [((AppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController presentViewController:_alertC animated:YES completion:_block]; } #pragma mark - 摄像头权限 +( BOOL )checkCameraAuthority{ return [self checkAuthority:[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]]; } +( void )cameraAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;{ if ([self checkCameraAuthority]) { if (_success) { _success(); } } else { [self showAlertController:_fail device:@ "相机" ]; } } #pragma mark - 麦克风权限 +( BOOL )checkAudioAuthority{ return [self checkAuthority:[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]]; } +( void )audioAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{ if ([self checkAudioAuthority]) { if (_success) { _success(); } } else { [self showAlertController:_fail device:@ "麦克风" ]; } } #pragma mark - 相册权限 +( BOOL )checkAlbumAuthority{ return [ALAssetsLibrary authorizationStatus] == ALAuthorizationStatusAuthorized; } +( void )albumAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail; { if ([self checkAlbumAuthority]) { if (_success) { _success(); } } else { [self showAlertController:_fail device:@ "照片" ]; } } #pragma mark - 位置权限 +( BOOL )checkLocationAuthority { return [CLLocationManager locationServicesEnabled]; } +( void )locationAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{ if ([self checkLocationAuthority]) { if (_success) { _success(); } } else { [self showAlertController:_fail device:@ "位置" ]; } } #pragma mark - 推送通知权限 +( BOOL )checkPushNotificationAuthority { return [[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone; } +( void )pushNotificationAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{ if ([self checkAlbumAuthority]) { if (_success) { _success(); } } else { [self showAlertController:_fail device:@ "通知" ]; } } #pragma mark - 通讯录权限 +( BOOL )checkAddressBookAuthority { return ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized || ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined; } +( void )AddressBookAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{ if ([self checkAddressBookAuthority]) { if (_success) { _success(); } } else { [self showAlertController:_fail device:@ "通讯录" ]; } } |
最后有些时会遇到不弹出权限提示,或需要在提示框增加详细描述的时候,需要手动在info.plist加一些字段。
NSLocationWhenInUseUsageDescription 位置权限 使用期间 状态
NSLocationAlwaysUsageDescription 位置权限 始终 状态
下面这些我并没有都试,所以也不知道是否正确....
NSLocationUsageDescription 用于访问位置权限
NSCalendarsUsageDescription 用于访问日历权限
NSContactsUsageDescription 用于访问联络人
NSPhotoLibraryUsageDescription 用于访问相册
NSRemindersUsageDescription 用于访问提醒
麦克风权限:Privacy - Microphone Usage Description 是否允许此App使用你的麦克风?
相机权限: Privacy - Camera Usage Description 是否允许此App使用你的相机?
相册权限: Privacy - Photo Library Usage Description 是否允许此App访问你的媒体资料库?
通讯录权限: Privacy - Contacts Usage Description 是否允许此App访问你的通讯录?
蓝牙权限:Privacy - Bluetooth Peripheral Usage Description 是否许允此App使用蓝牙?
语音转文字权限:Privacy - Speech Recognition Usage Description 是否允许此App使用语音识别?
日历权限:Privacy - Calendars Usage Description 是否允许此App使用日历?
定位权限:Privacy - Location When In Use Usage Description 我们需要通过您的地理位置信息获取您周边的相关数据
定位权限: Privacy - Location Always Usage Description 我们需要通过您的地理位置信息获取您周边的相关数据