麦克风测试软件 ios,iOS开发麦克风权限判断

在iOS开发中,我们经常要使用系统的各种权限!我们需要向系统进行申请,如果不申请会直接造成APP的闪退!

首先我们需要在info.plist文件中,加入以下代码

NSMicrophoneUsageDescription

请允许使用麦克风进行*****

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

引入头文件

#import

加入以下权限判断代码

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];

}