iOS判断应用是否获取到系统相机 相册的授权 以及如何请求授权

本文介绍了在iOS开发中如何判断应用程序是否获得了系统相机和相册的使用授权,同时详细讲解了如何正确地向用户请求这些权限的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

</pre><pre code_snippet_id="1815940" snippet_file_name="blog_20160808_1_3554133" name="code" class="objc"><pre name="code" class="objc"> //判断当前应用是否能访问相册资源
    /*
     typedef NS_ENUM(NSInteger, ALAuthorizationStatus) {
     ALAuthorizationStatusNotDetermined = 0, 用户尚未做出了选择这个应用程序的问候
     ALAuthorizationStatusRestricted,        此应用程序没有被授权访问的照片数据。可能是家长控制权限。

     ALAuthorizationStatusDenied,            用户已经明确否认了这一照片数据的应用程序访问.
     ALAuthorizationStatusAuthorized         用户已授权应用访问照片数据.
     }
     */
<pre name="code" class="objc">首先导入 AssetsLibrary.framework AVFoundation库 导入头文件
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>

/**
 *  调用系统相机
 */
- (void)callCamera
{
    //判断是否已授权
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if (authStatus == ALAuthorizationStatusDenied||authStatus == ALAuthorizationStatusRestricted) {
            [self setAlertControllerWithTitle:@"提示" message:@"请前往设置->隐私->相机授权应用拍照权限" actionTitle:@"确定"];
            return ;
        }
    }
    // 判断是否可以打开相机
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:picker animated:YES completion:nil];
    } else {
        [self setAlertControllerWithTitle:@"提示" message:@"你没有相机" actionTitle:@"确定"];
    }
}
/**
 *  调用系统相册
 */
- (void)callPhoto
{
    //判断是否已授权
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        ALAuthorizationStatus authStatus = [ALAssetsLibrary authorizationStatus];
        if (authStatus == ALAuthorizationStatusDenied) {
            [self setAlertControllerWithTitle:@"提示" message:@"请前往设置->隐私->相册授权应用访问相册权限" actionTitle:@"确定"];
            return;
        }

    }
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //self.isReload = NO;
        [self presentViewController:picker animated:YES completion:nil];
    } else {
        [self setAlertControllerWithTitle:@"提示" message:@"你没有相册" actionTitle:@"确定"];
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值