判断相机权限是否被限制
需要导入 AVFoundation 类
-
#import <AVFoundation/AVFoundation.h>
-
-
-
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
用户对应用程序授权
另外,需要对相机进行判断是否被授权,而相册不需要判断是否授权。
因为相机没有授权的话不能被使用。
而相册的话,系统默认modol出界面提示
就不需要我们进行判断,提示用户了。
判断相机是否可以使用
以下是参考方法:
-
#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;
-
}