iOS 设置录音权限
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