一、带弹框的权限判断

.h

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

@interface ImagePicker : NSObject

// 访问相机

+ (void)showCameraFromVC:(UIViewController *)fromVC finishPicking:(FinishPicking)finishPicking;

// 访问相册

+ (void)showAlbumFromVC:(UIViewController *)fromVC finishPicking:(FinishPicking)finishPicking;

@end

.m

#import

#import

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 && _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;

}

推荐链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: 


大家都在找:

ios:ios17.5.1值得更新吗

Xcode:xcode模拟器

objective-c:subject

大家都在看: