iOS相机、麦克风等权限的判断与设置

25 篇文章 0 订阅
19 篇文章 0 订阅

一、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.”
那么设置权限声明的的方式如下:
屏幕快照 2017-01-09 下午7.52.07.png
我们需要点击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;
*/
/**
 检测相机的方法
 @param permissionGranted 相机授权成功执行的方法
 @param 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();
            }];
            break;
        }
        case AVAuthorizationStatusAuthorized:
        {
            //通过授权
            permissionGranted();
            break;
        }
        case AVAuthorizationStatusRestricted:
            //不能授权
            NSLog(@"不能完成授权,可能开启了访问限制");
        case AVAuthorizationStatusDenied:{
            //提示跳转到相机设置(这里使用了blockits的弹窗方法)
            UIAlertView *alert = [UIAlertView  bk_showAlertViewWithTitle:@"相机授权" message:@"跳转相机授权设置" cancelButtonTitle:@"取消" otherButtonTitles:@[@"设置"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                if (buttonIndex == 1) {
                    //请求授权
                    [self requetSettingForVideo];
                 }
             }];
            [alert show];
        }
            break;
        default:
            break;
    }
}
2.相册

这里针对于iOS8及其以后的系统相册检测方法,使用到的PHPhotoLibrary需要导入Photos框架。

#import <Photos/Photos.h>
//相册的授权状态
typedef NS_ENUM(NSInteger, PHAuthorizationStatus) {
    PHAuthorizationStatusNotDetermined = 0, // User has not yet made a choice with regards to this application
    PHAuthorizationStatusRestricted,        // This application is not authorized to access photo data.
                                            // The user cannot change this application’s status, possibly due to active restrictions
                                            //   such as parental controls being in place.
    PHAuthorizationStatusDenied,            // User has explicitly denied this application access to photos data.
    PHAuthorizationStatusAuthorized         // User has authorized this application to access photos data.
} PHOTOS_AVAILABLE_IOS_TVOS(8_0, 10_0);

/**
 检测访问相册的权限
 这里的方法适用于iOS8及其以后版本
 @param permissionGranted 相册授权成功执行的方法
 @param noPermission 相册授权失败或者未授权执行的方法
 */
+ (void)checkPhotoAlbumAuthorizationGrand:(void (^)())permissionGranted withNoPermission:(void (^)())noPermission{
    PHAuthorizationStatus photoAuthStatus = [PHPhotoLibrary authorizationStatus];
    switch (photoAuthStatus) {
        case PHAuthorizationStatusNotDetermined:
        {
            //第一次提示用户授权
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                status == PHAuthorizationStatusAuthorized ? permissionGranted() : noPermission();
            }];
            break;
        }
        case PHAuthorizationStatusAuthorized:
        {
            //已经通过授权
            permissionGranted();
            break;
        }
        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];
            break;
        }
        default:
            break;
    }
}

二、iOS应用跳转权限设置

在iOS8以后的系统中,跳转设置使用如下方法:

+ (void)requetSettingForAuth{
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([ [UIApplication sharedApplication] canOpenURL:url])
    {   
        [[UIApplication sharedApplication] openURL:url];
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值