参考博客:http://www.2cto.com/kf/201501/370447.html
http://www.2cto.com/kf/201406/312257.html
判断相机权限是否被限制
需要导入 AVFoundation 类
#import <AVFoundation/AVFoundation.h>
// 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
用户对应用程序授权
另外,需要对相机进行判断是否被授权,而相册不需要判断是否授权。
因为相机没有授权的话不能被使用。
而相册的话,系统默认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;
}
摄像头坏了话,我们可以直接跳到 从相册中选择照片