iOS关于相机相册权限修改

电脑杂谈  发布时间:2019-09-09 21:08:04  来源:网络整理

ios调用相机和相册_强制进入相机 相册 ios_ios 相机相册

iOS10出来以后强制进入相机 相册 ios,有一些童鞋提审应用时会出现因为没有对手机相册等权限的设定提示而被拒绝,以及发生调取本地相册相机等出现崩溃,这是苹果为了安全而设定的权限所造成的,解决的方法就是在 plist 文件里添加相应的获得权限。

配置权限:

相机权限:Privacy - Camera Usage Description 允许此权限从而使用相机功,这样就能拍摄视频,并且想要保存照片。

相册权限:Privacy - Photo Library Usage Description 允许此权限从而使用平台相册。

麦克风权限:Privacy - Microphone Usage Description 获取麦克风权限不然会崩,只有允许此权限从而录音。

在info.plist里减少一项,key从里面的三项任一项拷贝,然后运行后会出现授权的警告框,同意后就没有问题了。

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

info.plist中逐个添加 KEY直接复制 value的string字符串就是提示的文字 可以按照自己应该填写

ios调用相机和相册_ios 相机相册_强制进入相机 相册 ios

one.png

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

判断相机权限能否被限制

需要导入 AVFoundation 类

// 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  

ios 相机相册_强制进入相机 相册 ios_ios调用相机和相册

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

AVAuthorizationStatusRestricted  

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

AVAuthorizationStatusDenied  

用户拒绝对应用程序授权

AVAuthorizationStatusAuthorized  

用户对应用程序授权

另外,需要对相机进行判定是否被授权,而相册不需要判断能否授权。

因为相机没有授权的话不能被使用却没有任何有用的提醒。

ios调用相机和相册_ios 相机相册_强制进入相机 相册 ios

01E8E.png

可以自行按照判定修改成这种的提示

C95CA.png

而相册的话,系统默认modol出图标提示

7AD83.png

就不需要我们进行判定强制进入相机 相册 ios,提示用户了。

强制进入相机 相册 ios_ios调用相机和相册_ios 相机相册

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


- (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;
    }
}

判断相机是否可以使用

以下是参考方式:

// 判断设备是否有  
- (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];


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/ruanjian/article-122317-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...