查询相册和摄像头的使用权限

当你第一次在应用中打开相册的时候,系统会提示你是否允许用户访问相册,如果你选择不允许的话,你以后在该应用中将无法访问相册。如果想要重新允许,那么需要去“隐私设置”里面去设置。在程序中怎么获取用户是否拥有对相册的访问权限,然后做相应地操作呢,下面列出了相册的权限

iOS8之前

// 所在的库:<AssetsLibrary/AssetsLibrary.h>

typedef NS_ENUM(NSInteger, ALAuthorizationStatus) {
    ALAuthorizationStatusNotDetermined = 0, // 用户还没有做出选择
    ALAuthorizationStatusRestricted,        // 家长控制,不允许访问
    ALAuthorizationStatusDenied,            // 用户拒绝当前应用访问相册,我们需要提醒用户打开访问开关
    ALAuthorizationStatusAuthorized         // 用户允许当前应用访问相册
}

// 获取当前照片库授权状态的方法,可以通过判断这个状态,来决定是否要提醒用户到设置中开启服务
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];

iOS8之后

// 所在的库:<Photos/Photos.h>

typedef NS_ENUM(NSInteger, PHAuthorizationStatus) {
    PHAuthorizationStatusNotDetermined = 0, // 用户还没有做出选择
    PHAuthorizationStatusRestricted,        // 家长控制,不允许访问
    PHAuthorizationStatusDenied,            // 用户拒绝当前应用访问相册,我们需要提醒用户打开访问开关
    PHAuthorizationStatusAuthorized         // 用户允许当前应用访问相册
}

// 获取当前照片库授权状态的方法,可以通过判断这个状态,来决定是否要提醒用户到设置中开启服务
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
在ios7之前摄像头是一直可以访问的,隐私设置选项中没有关闭相应软件摄像头功能的选项。在ios7以后摄像头和相册一样增加了访问权限的设置,应用中第一次访问摄像头的时候,系统会询问你是否授权应用访问你的摄像头。摄像头的权限和相册的权限基本上一样
// 所在的库:<AVFoundation/AVFoundation.h>

typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {
    AVAuthorizationStatusNotDetermined = 0, // 用户还没有做出选择
    AVAuthorizationStatusRestricted,        // 家长控制,不允许访问
    AVAuthorizationStatusDenied,            // 用户拒绝当前应用访问相册,我们需要提醒用户打开访问开关
    AVAuthorizationStatusAuthorized         // 用户允许当前应用访问相机
}

// 获取当前摄像头授权状态的方法,可以通过判断这个状态,来决定是否要提醒用户到设置中开启服务
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
如果是iOS8之后,则可以直接跳转到APP设置面板
+ (void)goAppSetting
{
    // 打开应用设置面板
    NSURL *appSettingUrl = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    UIApplication *app = [UIApplication sharedApplication];
    if ([app canOpenURL:appSettingUrl])
    {
        if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0)
        {
            [app openURL:appSettingUrl options:@{} completionHandler:^(BOOL success) {
                if (!success) [self showJumpErrorAlert];
            }];
        }
        else
        {
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wdeprecated-declarations"
            [app openURL:appSettingUrl];
#pragma clang diagnostic pop
        }
    }
    else
    {
        [self showJumpErrorAlert];
    }
}

+ (void)showJumpErrorAlert
{
    [DVVAlertView showAlertWithTitle:@"跳转失败"
                             message:@"请手动到设置中打开服务"
                        buttonTitles:@[@"取消"]
                          completion:nil];
}
iOS10需要在info.plist中加入访问相机和相册的使用权限
相机 
Key:Privacy - Camera Usage Description
Type:String
相册
Key:Privacy - Photo Library Usage Description
TypeString
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值