1.問題描述:在首次進入控制器就彈出是否允許開啟麥克風的權限提示,如果選擇不允許,可在下次調用時提示用戶去“設置”頁面進行再次開啟;
2.代碼
+ (BOOL)checkPermission{ // AVAudioSessionRecordPermission permission = [[AVAudioSession sharedInstance] recordPermission]; // return permission == AVAudioSessionRecordPermissionGranted; __block BOOL bCanRecord = YES; if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 7.0) { AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]; if (videoAuthStatus == AVAuthorizationStatusNotDetermined) {// 未詢問用戶是否授權 AVAudioSession *audioSession = [AVAudioSession sharedInstance]; if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) { [audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) { if (granted) { bCanRecord = YES; } else { bCanRecord = NO; } }]; } } else if(videoAuthStatus == AVAuthorizationStatusRestricted || videoAuthStatus == AVAuthorizationStatusDenied) { // 未授權 [self showSetAlertView]; } else{ // 已授權 NSLog(@"已授權"); } } return bCanRecord; } //提示用戶進行麥克風使用授權 + (void)showSetAlertView { [LGAlert alertWithTitle:@"麥克風權限未開啟" message:@"麥克風權限未開啟,請進入系統【設置】>【隱私】>【麥克風】中打開開關,開啟麥克風功能" cancelTitle:@"取消" cancelBlock:^{ } confirmTitle:@"去設置" confirmBlock:^{ //跳入當前App設置界面 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; }]; };
3.參考的主要網址:https://www.jianshu.com/p/e952ef3e33bc
