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

判断相机权限是否被限制

需要导入   AVFoundation 类

[objc] view plain copy
  1. #import <AVFoundation/AVFoundation.h>  

[objc] view plain copy
  1. //    iOS 判断应用是否有使用相机的权限  
  2.       
  3.     NSString *mediaType = AVMediaTypeVideo;//读取媒体类型  
  4.     AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态  
  5.     if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){  
  6.         NSString *errorStr = @"应用相机权限受限,请在设置中启用";  
  7.         [[HUDHelper getInstance] showErrorTipWithLabel:errorStr view:self.navigationController.view];  
  8.         return;  
  9.     }  


如果状态是一个枚举

[objc] view plain copy
  1. typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {  
  2.     AVAuthorizationStatusNotDetermined = 0,  
  3.     AVAuthorizationStatusRestricted,  
  4.     AVAuthorizationStatusDenied,  
  5.     AVAuthorizationStatusAuthorized  
  6. } NS_AVAILABLE_IOS(7_0);  

[objc] view plain copy
  1. AVAuthorizationStatusNotDetermined  
用户还没有对应用程序授权进行操作

[objc] view plain copy
  1. AVAuthorizationStatusRestricted  
还没有授权访问的照片数据。

[objc] view plain copy
  1. AVAuthorizationStatusDenied  
用户拒绝对应用程序授权

[objc] view plain copy
  1. AVAuthorizationStatusAuthorized  
用户对应用程序授权



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

因为相机没有授权的话不能被使用。


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

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




判断相机是否可以使用

以下是参考方法:

[objc] view plain copy
  1. #pragma mark - 摄像头和相册相关的公共类  
  2. // 判断设备是否有摄像头  
  3. - (BOOL) isCameraAvailable{  
  4.     return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];  
  5. }  
  6.   
  7. // 前面的摄像头是否可用  
  8. - (BOOL) isFrontCameraAvailable{  
  9.     return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];  
  10. }  
  11.   
  12. // 后面的摄像头是否可用  
  13. - (BOOL) isRearCameraAvailable{  
  14.     return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];  
  15. }  

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

[objc] view plain copy
  1. if (![self isFrontCameraAvailable]) {  
  2.         //判断相机是否可用  
  3.         NSString *errorStr = @"相机出现问题,将跳转到相册选择照片";  
  4.         [[HUDHelper getInstance] showErrorTipWithLabel:errorStr view:self.navigationController.view];  
  5.         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{  
  6.             [self openPhotoLibrary];  
  7.         });  
  8.         return;  
  9.     }  
posted @ 2016-02-28 11:21  AlvinsBlog  阅读(1908)  评论(0编辑  收藏  举报