一、带弹框的权限判断

.h

typedef void (^FinishPicking)(UIImage *image);

@interface ImagePicker : NSObject <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
// 访问相机
+ (void)showCameraFromVC:(UIViewController *)fromVC finishPicking:(FinishPicking)finishPicking;
// 访问相册
+ (void)showAlbumFromVC:(UIViewController *)fromVC finishPicking:(FinishPicking)finishPicking;
@end

.m

#import <Photos/PHPhotoLibrary.h>
#import <AVFoundation/AVCaptureDevice.h>

static FinishPicking _finishPicking;

@implementation ImagePicker

// 访问相机功能
+ (void)showCameraFromVC:(UIViewController *)fromVC finishPicking:(FinishPicking)finishPicking {
    _finishPicking = [finishPicking copy];
    
    // 判断相机授权弹出访问权限提示
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
        dispatch_async(dispatch_get_main_queue(),^{
            if (granted) { // 授权成功
                [self openCamera:fromVC];
            } else { // 拒绝授权
                [self showCameraPermissionsTipPopView:fromVC];
            }
        });
    }];
}

// 打开相机
+ (void)openCamera:(UIViewController *)fromVC {
    if (@available(iOS 11.0, *)) {
        [UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
    }
    
    UIImagePickerController *ipv = [[UIImagePickerController alloc] init];
    ipv.delegate = [self self];
    ipv.sourceType = UIImagePickerControllerSourceTypeCamera;
    ipv.modalPresentationStyle = UIModalPresentationFullScreen;
    [fromVC presentViewController:ipv animated:YES completion:nil];
}

// 不能打开相机时提示
+ (void)showCameraPermissionsTipPopView:(UIViewController *)fromVC {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请在手机系统设置开启权限" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openSettings];
    }]];
    [fromVC presentViewController:alert animated:YES completion:nil];
}


// 访问相册功能
+ (void)showAlbumFromVC:(UIViewController *)fromVC finishPicking:(FinishPicking)finishPicking {
    _finishPicking = [finishPicking copy];
    
    // 判断相册授权弹出访问权限提示
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        dispatch_async(dispatch_get_main_queue(),^{
            if (status == PHAuthorizationStatusAuthorized) {
                [self openPhotoAlbum:fromVC];
            } else {
                [self showPhotoAlbumPermissionsTipPopView:fromVC];
            }
        });
    }];
}

// 打开相册
+ (void)openPhotoAlbum:(UIViewController *)fromVC {
    if (@available(iOS 11.0, *)) {
        [UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
    }
    
    UIImagePickerController *ipv = [[UIImagePickerController alloc] init];
    ipv.delegate = [self self];
    ipv.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    ipv.modalPresentationStyle = UIModalPresentationFullScreen;
    [fromVC presentViewController:ipv animated:YES completion:nil];
}

// 不能打开相册时提示
+ (void)showPhotoAlbumPermissionsTipPopView:(UIViewController *)fromVC {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请在手机系统设置开启此权限" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openSettings];
    }]];
    [fromVC presentViewController:alert animated:YES completion:nil];
}

// 打开设置
+ (void)openSettings {
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        if (@available(iOS 10.0, *)) {
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {}];
        } else {
            [[UIApplication sharedApplication] openURL:url];
        }
    }
}

// 回调
+ (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    if (image &amp;&amp; _finishPicking) {
        _finishPicking(image);
        _finishPicking = nil;
    }

    [picker dismissViewControllerAnimated:YES completion:^{
        if (@available(iOS 11.0, *)) {
            [UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }];
}

+ (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^{
        if (@available(iOS 11.0, *)) {
            [UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }];
}

@end

二、无弹框的权限判断

PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus];
    if (authorizationStatus == PHAuthorizationStatusAuthorized) {
        return YES;
    } else {
        return NO;
    }
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusAuthorized) {
        return YES;
    } else {
        return NO;
    }

原文地址:https://blog.csdn.net/junjie_zhang/article/details/125557714

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_18217.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注