iOS:麦克风权限检测和获取

一、检测

该方法是用来判断麦克风是否进行过授权,如果授权过就直接进行需要的功能操作;如果没有进行授权,那么就要获取授权。

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
      switch (authStatus) {
          case AVAuthorizationStatusNotDetermined:
          //没有询问是否开启麦克风
              break;
          case AVAuthorizationStatusRestricted:
          //未授权,家长限制
             break;
         case AVAuthorizationStatusDenied:
         //玩家未授权
             break;
         case AVAuthorizationStatusAuthorized:
         //玩家授权
             break;
         default:
             break;
     }
 }

 

二、获取

注意:该方法只有在未询问过用户授权的情况下生效,也就是只能第一次询问的时候调用,如果第一次调用时点击了不允许,再次监测状态时调用无效,只能提示用户去设置中打开开关。

//麦克风权限(一些操作需要回到主线程进行)
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted)
{
     if (!granted) {
             dispatch_async(dispatch_get_main_queue(), ^{
                
            }
            else{
        
         dispatch_async(dispatch_get_main_queue(), ^{
            }
} }];

 

三、弹窗提示

-(void)showAlertView{
     [[UIAlertView alloc] initWithTitle:nil
                      message:@"xxx需要访问您的麦克风。\n请启用麦克风-设置/隐私/麦克风"
                      delegate:nil
                      cancelButtonTitle:@"确定"
                      otherButtonTitles:nil] show];    
}

 

posted @ 2017-08-03 17:32  XYQ全哥  阅读(2009)  评论(0编辑  收藏  举报