iOS开发麦克风权限判断

作者: 随缘吖 | 来源:发表于2019-04-25 18:30 被阅读0次

    在iOS开发中,我们经常要使用系统的各种权限!我们需要向系统进行申请,如果不申请会直接造成APP的闪退!
    首先我们需要在info.plist文件中,加入以下代码

    <key>NSMicrophoneUsageDescription</key>
    <string>请允许使用麦克风进行*****</string>
    

    在进行麦克风使用时候,我们需要对权限进行判断,我们是否有权限使用麦克风
    引入头文件

    #import <AVFoundation/AVFoundation.h>
    

    加入以下权限判断代码

      AVAuthorizationStatus microPhoneStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
        switch (microPhoneStatus) {
            case AVAuthorizationStatusDenied:
            case AVAuthorizationStatusRestricted:
            {
                // 被拒绝
                [self goMicroPhoneSet];
            }
                break;
            case AVAuthorizationStatusNotDetermined:
            {
                // 没弹窗
                [self requestMicroPhoneAuth];
            }
                break;
            case AVAuthorizationStatusAuthorized:
            {
                // 有授权
            }
                break;
    
            default:
                break;
        }
    

    如果还有进行申请,要进行权限申请

    -(void) requestMicroPhoneAuth
    {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
    
        }];
    }
    

    如果用户没有允许,可以进行弹窗提示,进入设置页面,让用户进行选择

    -(void) goMicroPhoneSet
    {
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"您还没有允许麦克风权限" message:@"去设置一下吧" preferredStyle:UIAlertControllerStyleAlert];
    
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
        }];
        UIAlertAction * setAction = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                [UIApplication.sharedApplication openURL:url options:nil completionHandler:^(BOOL success) {
    
                }];
            });
        }];
    
        [alert addAction:cancelAction];
        [alert addAction:setAction];
    
        [self presentViewController:alert animated:YES completion:nil];
    }
    

    相关文章

      网友评论

        本文标题:iOS开发麦克风权限判断

        本文链接:https://www.haomeiwen.com/subject/mcftnqtx.html