iOS关于相机相册权限设置 您所在的位置:网站首页 ios的qq无法访问相片 iOS关于相机相册权限设置

iOS关于相机相册权限设置

2023-07-06 09:31| 来源: 网络整理| 查看: 265

iOS10出来之后,有一些童鞋提审应用时会出现因为没有对相机相册等权限的设置提醒而被拒绝,以及出现调取本地相册相机等出现崩溃,这是苹果为了安全而设置的权限所导致的,解决的办法就是在 plist 文件里添加相应的获取权限。

配置权限: 相机权限:Privacy - Camera Usage Description 允许此权限才能使用相机功,这样才能录制视频,并且想要保存图片。 相册权限:Privacy - Photo Library Usage Description 允许此权限才能使用系统相册。 麦克风权限:Privacy - Microphone Usage Description 获取麦克风权限不然会崩,只有允许此权限才能录音。 在info.plist里增加一项,key从上面的三项任一项拷贝,然后运行后会出现授权的警示框,同意后就没有问题了。

NSPhotoLibraryUsageDescription App需要您的同意,才能访问相册 NSCameraUsageDescription App需要您的同意,才能访问相机 NSMicrophoneUsageDescription App需要您的同意,才能访问麦克风 NSLocationUsageDescription App需要您的同意,才能访问位置 NSLocationWhenInUseUsageDescription App需要您的同意,才能在使用期间访问位置 NSLocationAlwaysUsageDescription App需要您的同意,才能始终访问位置 NSCalendarsUsageDescription App需要您的同意,才能访问日历 NSRemindersUsageDescription App需要您的同意,才能访问提醒事项 NSMotionUsageDescription App需要您的同意,才能访问运动与健身 NSHealthUpdateUsageDescription App需要您的同意,才能访问健康更新 NSHealthShareUsageDescription App需要您的同意,才能访问健康分享 NSBluetoothPeripheralUsageDescription App需要您的同意,才能访问蓝牙 NSAppleMusicUsageDescription App需要您的同意,才能访问媒体资料库

info.plist中逐个添加 KEY直接复制 value的string字符串就是提示的文字 可以根据自己需要填写

one.png

判断相机权限是否被限制,判断相机是否可以使用

判断相机权限是否被限制

需要导入 AVFoundation 类

import

// iOS 判断应用是否有使用相机的权限

NSString *mediaType = AVMediaTypeVideo;//读取媒体类型 AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态 if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){ NSString *errorStr = @"应用相机权限受限,请在设置中启用"; [[HUDHelper getInstance] showErrorTipWithLabel:errorStr view:self.navigationController.view]; return; }

如图状态是一个枚举

typedef NS_ENUM(NSInteger, AVAuthorizationStatus) { AVAuthorizationStatusNotDetermined = 0, AVAuthorizationStatusRestricted, AVAuthorizationStatusDenied, AVAuthorizationStatusAuthorized } NS_AVAILABLE_IOS(7_0); AVAuthorizationStatusNotDetermined

用户还没有对应用程序授权进行操作

AVAuthorizationStatusRestricted

还没有授权访问的照片数据。

AVAuthorizationStatusDenied

用户拒绝对应用程序授权

AVAuthorizationStatusAuthorized

用户对应用程序授权

另外,需要对相机进行判断是否被授权,而相册不需要判断是否授权。 因为相机没有授权的话不能被使用却没有任何有用的提示。

01E8E.png

可以自行根据判断设置成这样的提示

C95CA.png

而相册的话,系统默认modol出界面提示

7AD83.png

就不需要我们进行判断,提示用户了。

上述视图判断逻辑代码如下

- (void)PhotoClick:(UIButton *)button{ switch (button.tag) { case 1:{ DLog(@"拍照"); [self.darkView removeFromSuperview]; UIImagePickerController *pick = [[UIImagePickerController alloc]init]; pick.sourceType = UIImagePickerControllerSourceTypeCamera; pick.delegate = self; //判断是否有相机权限 NSString *mediaType = AVMediaTypeVideo;//读取媒体类型 AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态 if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){ NSString *errorStr = @"应用相机权限受限,请在iPhone的“设置-隐私-相机”选项中,允许好享玩访问你的相机。"; DLog(@"相机不可用"); //必须使用present 方法 //[self presentViewController:pick animated:YES completion:nil]; [self showAlertControllerWithMessage:errorStr]; } else { DLog(@"相机可用"); //必须使用present 方法 [self presentViewController:pick animated:YES completion:nil]; } } break; case 2:{ DLog(@"相册"); [self.darkView removeFromSuperview]; UIImagePickerController *pick = [[UIImagePickerController alloc]init]; pick.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; pick.delegate = self; //必须使用present 方法调用相册 相机也一样 [self presentViewController:pick animated:YES completion:nil]; } break; default: break; } }

判断相机是否可以使用

以下是参考方法:

pragma mark - 摄像头和相册相关的公共类 // 判断设备是否有摄像头 - (BOOL) isCameraAvailable{ return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; } // 前面的摄像头是否可用 - (BOOL) isFrontCameraAvailable{ return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]; } // 后面的摄像头是否可用 - (BOOL) isRearCameraAvailable{ return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]; }

相应的我们需要判断用户的摄像头是否是坏的,以防程序crash

if (![self isFrontCameraAvailable]) { //判断相机是否可用 NSString *errorStr = @"相机出现问题,将跳转到相册选择照片"; [[HUDHelper getInstance] showErrorTipWithLabel:errorStr view:self.navigationController.view]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self openPhotoLibrary]; }); return; }

如果摄像头坏了的话,我们可以直接跳到从相册中选择照片。

判断用户访问相册权限

iOS10以上系统 首先,需在工程对应的plist文件内添加“Privacy - Photo Library Usage Description”这个key,同时设置其值为“App needs your permission to access the Photo”类似这样的说明。

//获取相册访问权限 PHAuthorizationStatus photoStatus = [PHPhotoLibrary authorizationStatus];

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { dispatch_async(dispatch_get_main_queue(), ^{ switch (status) { case PHAuthorizationStatusAuthorized: //已获取权限 break; case PHAuthorizationStatusDenied: //用户已经明确否认了这一照片数据的应用程序访问 break; case PHAuthorizationStatusRestricted://此应用程序没有被授权访问的照片数据。可能是家长控制权限 break; default://其他。。。 break; } }); }];


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有