前言

今天接到一个任务,检测应用是否有麦克风的权限,网上找了很多资料,然后终于明白了检测和获取的区别

检测权限

检测就是判断用户是否打开了麦克风的权限
判断方法是这样的

 1 AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
 2     switch (authStatus) {
 3         case AVAuthorizationStatusNotDetermined:
 4         //没有询问是否开启麦克风
 5             flag = 1;
 6             break;
 7         case AVAuthorizationStatusRestricted:
 8         //未授权,家长限制
 9             flag = 0;
10             break;
11         case AVAuthorizationStatusDenied:
12         //玩家未授权
13             flag = 0;
14             break;
15         case AVAuthorizationStatusAuthorized:
16         //玩家授权
17             flag = 2;
18             break;
19         default:
20             break;
21     }
22     return flag;
23 }

获取权限

获取权限就会弹一个框,让用户选择是否开启这个权限

1 [audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
2              if (granted) {
3                    bCanRecord = YES;
4                 }
5                 else {
6                     bCanRecord = NO;
7                    }
8 ];