前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS相机、麦克风等权限的判断与设置

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

作者头像
梧雨北辰
发布2018-04-24 14:50:03
3.5K0
发布2018-04-24 14:50:03
举报
一、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框架

代码语言:javascript
复制
#import <AVFoundation/AVFoundation.h>
代码语言:javascript
复制
 /**
//相机、麦克风的授权状态
typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {
    AVAuthorizationStatusNotDetermined = 0,//未询问过用户是否授权
    AVAuthorizationStatusRestricted, //未授权,例如家长控制
    AVAuthorizationStatusDenied, //未授权,用户曾选择过拒绝授权
    AVAuthorizationStatusAuthorized //已经授权
} NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
*/
//AVMediaTypeVideo:相机权限
//AVMediaTypeAudio:麦克风权限
代码语言:javascript
复制
/**
 检测相机的方法
 @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框架。

代码语言:javascript
复制
#import <Photos/Photos.h>
代码语言:javascript
复制
//相册的授权状态
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);
代码语言:javascript
复制
/**
 检测访问相册的权限
 这里的方法适用于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以后的系统中,跳转设置使用如下方法:

代码语言:javascript
复制
+ (void)requetSettingForAuth{
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([ [UIApplication sharedApplication] canOpenURL:url])
    {   
        [[UIApplication sharedApplication] openURL:url];
    }
}
三、使用注意

我们在检测授权的时候弹窗会有授权和不授权的回调,有时候我们会在这里处理一些自定义UI问题,这里一定要在主线程中进行,否则会出现崩溃等问题,回到主线程中的操作如下:

代码语言:javascript
复制
  dispatch_async(dispatch_get_main_queue(), ^{
           //处理UI问题            
});
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.01.09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、iOS应用权限检测
    • 1.相机与麦克风
      • 2.相册
      • 二、iOS应用跳转权限设置
      • 三、使用注意
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档