我开了ios10 开启麦克风权限和相机权限,为什么还是告诉我

3067人阅读
一、iOS应用权限检测
在涉及到这个问题的时候,首先为了适配iOS10系统,我们必须首先在info.plist文件中声明将要用到的权限,否则将会引起崩溃如下:
“This app has crashed because it attempted to access privacy-sensitive data without a usage description.
The app’s Info.plist must contain an NSMicrophoneUsageDescription key with a string value explaining to the user how the app uses this data.”
那么设置权限声明的的方式如下:
我们需要点击Info.plist中加号,增加需要授权key值并填写相应的权限使用声明。
1.相机与麦克风
检测相机与麦克风权限需要导入AVFoundataion框架
#import &AVFoundation/AVFoundation.h&
//相机、麦克风的授权状态
typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {
AVAuthorizationStatusNotDetermined = 0,//未询问过用户是否授权
AVAuthorizationStatusRestricted, //未授权,例如家长控制
AVAuthorizationStatusDenied, //未授权,用户曾选择过拒绝授权
AVAuthorizationStatusAuthorized //已经授权
} NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
检测相机的方法
permissionGranted 相机授权成功执行的方法
noPermission 相机授权失败或者未授权执行的方法
+ (void)checkCameraAuthorizationGrand:(void (^)())permissionGranted withNoPermission:(void (^)())noPermission{
AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (videoAuthStatus) {
case AVAuthorizationStatusNotDetermined:
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
granted ? permissionGranted() : noPermission();
case AVAuthorizationStatusAuthorized:
permissionGranted();
case AVAuthorizationStatusRestricted:
NSLog(@"不能完成授权,可能开启了访问限制");
case AVAuthorizationStatusDenied:{
UIAlertView *alert = [UIAlertView
bk_showAlertViewWithTitle:@"相机授权" message:@"跳转相机授权设置" cancelButtonTitle:@"取消" otherButtonTitles:@[@"设置"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
if (buttonIndex == 1) {
[self requetSettingForVideo];
[alert show];
这里针对于iOS8及其以后的系统相册检测方法,使用到的PHPhotoLibrary需要导入Photos框架。
#import &Photos/Photos.h&
typedef NS_ENUM(NSInteger, PHAuthorizationStatus) {
PHAuthorizationStatusNotDetermined = 0,
PHAuthorizationStatusRestricted,
PHAuthorizationStatusDenied,
PHAuthorizationStatusAuthorized
} PHOTOS_AVAILABLE_IOS_TVOS(8_0, 10_0);
检测访问相册的权限
这里的方法适用于iOS8及其以后版本
permissionGranted 相册授权成功执行的方法
noPermission 相册授权失败或者未授权执行的方法
+ (void)checkPhotoAlbumAuthorizationGrand:(void (^)())permissionGranted withNoPermission:(void (^)())noPermission{
PHAuthorizationStatus photoAuthStatus = [PHPhotoLibrary authorizationStatus];
switch (photoAuthStatus) {
case PHAuthorizationStatusNotDetermined:
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
status == PHAuthorizationStatusAuthorized ? permissionGranted() : noPermission();
case PHAuthorizationStatusAuthorized:
permissionGranted();
case PHAuthorizationStatusRestricted:
NSLog(@"不能完成授权,可能开启了访问限制");
case PHAuthorizationStatusDenied:{
UIAlertView *alert = [UIAlertView
bk_showAlertViewWithTitle:@"相册授权" message:@"跳转相册授权设置" cancelButtonTitle:@"取消" otherButtonTitles:@[@"设置"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
if (buttonIndex == 1) {
[self requetSettingForPhotoAlbum];
[alert show];
二、iOS应用跳转权限设置
在iOS8以后的系统中,跳转设置使用如下方法:
+ (void)requetSettingForAuth{
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([ [UIApplication sharedApplication] canOpenURL:url])
[[UIApplication sharedApplication] openURL:url];
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:14931次
排名:千里之外
原创:23篇
(2)(4)(2)(2)(3)(2)(1)(5)(1)(1)

我要回帖

更多关于 ios 开启麦克风权限 的文章

 

随机推荐