版本记录
版本号
时间
V1.0
2017.07.21
前言
功能需求
判断照相机和麦克风的权限。
功能实现
下面我们就用#import 这个库,来获取照相机和麦克风的权限。
下面我们就直接看代码。
#import "JJAuthorityVC.h"
#import
@interface JJAuthorityVC ()
@end
@implementation JJAuthorityVC
#pragma mark - Override Private Function
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//判断照相机和,麦克风权限
NSString *mediaType = AVMediaTypeVideo;//读取媒体类型
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态
if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
NSString *errorStr = @"应用相机权限受限,请在设置中启用";
[self pushAlertWithMessage:errorStr];
return;
}
else {
[self pushAlertWithMessage:@"可以使用相机"];
}
mediaType = AVMediaTypeAudio;//读取媒体类型
authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态
if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
NSString *errorStr = @"麦克风权限受限,请在设置中启用";
[self pushAlertWithMessage:errorStr];
return;
}
else{
[self pushAlertWithMessage:@"可以使用麦克风"];
}
}
#pragma mark - Object Private Function
- (void)pushAlertWithMessage:(NSString *)message
{
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ensureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
return;
}];
[alertVC addAction:ensureAction];
[self presentViewController:alertVC animated:YES completion:nil];
}
@end
在运行代码后,会发现控制台打印如下消息。
2017-07-21 20:20:03.718718+0800 JJOC[4737:1350171] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-07-21 20:20:03.720009+0800 JJOC[4737:1350171] [MC] Reading from public effective user settings.
找到原因,其实是info.plist中需要配置一下权限问题,如下图所示。
这里配置的就是相机和麦克风的权限。
配置权限
细看会发现,这里面有很多有关权限方面的配置,更体现了苹果对应安全性的重视。
配置权限
功能效果
下面看一下功能效果。
功能效果
这里的功能效果,是已经默认有了权限,提示可以使用相机,在具体工程中,如果在设置中关闭了这两个权限,就会提示应用相机权限受限,请在设置中启用和麦克风权限受限,请在设置中启用。
后记
未完,待续~~~~
夜景