ios 錄音權(quán)限的判斷和處理


- (BOOL)checkMicrophonePermission{
    
    //    AVAudioSessionRecordPermission permission = [[AVAudioSession sharedInstance] recordPermission];
    //    return permission == AVAudioSessionRecordPermissionGranted;
    __block BOOL bCanRecord = NO;
    if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 7.0) {
        AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
        if (videoAuthStatus == AVAuthorizationStatusNotDetermined) {// 未詢問(wèn)用戶是否授權(quán)
            AVAudioSession *audioSession = [AVAudioSession sharedInstance];
            if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
                [audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
                    if (granted) {//用戶選擇允許
                        bCanRecord = YES;
                        NSLog(@"用戶選擇“允許”打開(kāi)麥克風(fēng)權(quán)限");
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self startRecordAudioSetup];
                        });
                        if (self.delegate && [self.delegate respondsToSelector:@selector(clickAllowOpenMicrophoneAuthorization)]) {
                            [self.delegate clickAllowOpenMicrophoneAuthorization];
                        }
                        
                    } else {//用戶選擇不允許
                        bCanRecord = NO;
                        NSLog(@" 用戶選擇“不允許”打開(kāi)麥克風(fēng)權(quán)限");
                        [self popUpMicrophonePermissionAlertView];//彈出自己自定義的窗
                        
                        if (self.delegate && [self.delegate respondsToSelector:@selector(clickNotAllowOpenMicrophoneAuthorization)]) {
                            [self.delegate clickNotAllowOpenMicrophoneAuthorization];
                        }
                    }
                }];
            }
        } else if(videoAuthStatus == AVAuthorizationStatusRestricted || videoAuthStatus == AVAuthorizationStatusDenied) {
            bCanRecord = NO;//用戶在第一次系統(tǒng)彈窗后選擇不允許之后,再次錄音的時(shí)候會(huì)走這里“麥克風(fēng)權(quán)限未授權(quán)”
            // 未授權(quán)
            NSLog(@"未授權(quán)");
            [self popUpMicrophonePermissionAlertView];//彈出自己自定義的窗
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(microphoneUnauthorized)]) {
                [self.delegate microphoneUnauthorized];
            }
        } else{
            bCanRecord = YES;
            // 已授權(quán)
            NSLog(@"已授權(quán)");
            [self startRecordAudioSetup];
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(microphoneAuthorized)]) {
                [self.delegate microphoneAuthorized];
            }
            
        }
    }
    return bCanRecord;
}


//彈出自定義開(kāi)啟麥克風(fēng)權(quán)限的提示框
- (void)popUpMicrophonePermissionAlertView{
    
    dispatch_async(dispatch_get_main_queue(), ^{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"麥克風(fēng)權(quán)限未開(kāi)啟"message:@"麥克風(fēng)權(quán)限未開(kāi)啟,請(qǐng)進(jìn)入系統(tǒng)【設(shè)置】>【隱私】>【麥克風(fēng)】中打開(kāi)開(kāi)關(guān),開(kāi)啟麥克風(fēng)功能" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"確定");
        //跳入當(dāng)前App設(shè)置界面
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    
    [self  presentViewController:alertController animated:YES completion:nil];
    });
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容