zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    头文件#ifndef #define #endif使用
    Django框架【form组件】
    数据库【mysql】之pymysql
    数据库【mysql篇】典型的一些练习题目
    Python开发【socket篇】解决粘包
    Python开发【内置模块篇】os模块
    Python开发【内置模块篇】日志模块
    Python开发【内置模块篇】configparser
    Python开发【内置模块篇】collections
    Python开发【内置模块篇】datetime
  • 原文地址:https://www.cnblogs.com/wusang/p/9114964.html
Copyright © 2011-2022 走看看