《iOS判断应用是否获取到系统相机 相册的授权 以及如何请求授权》
</pre><pre code_snippet_id="1815940" snippet_file_name="blog_20160808_1_3554133" name="code" class="objc"><pre name="code" class="objc"> //判断当前应用是否能访问相册资源
/*
typedef NS_ENUM(NSInteger, ALAuthorizationStatus) {
ALAuthorizationStatusNotDetermined = 0, 用户尚未做出了选择这个应用程序的问候
ALAuthorizationStatusRestricted, 此应用程序没有被授权访问的照片数据。可能是家长控制权限。
ALAuthorizationStatusDenied, 用户已经明确否认了这一照片数据的应用程序访问.
ALAuthorizationStatusAuthorized 用户已授权应用访问照片数据.
}
*/
<pre name="code" class="objc">首先导入 AssetsLibrary.framework AVFoundation库 导入头文件
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
/**
* 调用系统相机
*/
- (void)callCamera
{
//判断是否已授权
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == ALAuthorizationStatusDenied||authStatus == ALAuthorizationStatusRestricted) {
[self setAlertControllerWithTitle:@"提示" message:@"请前往设置->隐私->相机授权应用拍照权限" actionTitle:@"确定"];
return ;
}
}
// 判断是否可以打开相机
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
} else {
[self setAlertControllerWithTitle:@"提示" message:@"你没有相机" actionTitle:@"确定"];
}
}
/**
* 调用系统相册
*/
- (void)callPhoto
{
//判断是否已授权
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
ALAuthorizationStatus authStatus = [ALAssetsLibrary authorizationStatus];
if (authStatus == ALAuthorizationStatusDenied) {
[self setAlertControllerWithTitle:@"提示" message:@"请前往设置->隐私->相册授权应用访问相册权限" actionTitle:@"确定"];
return;
}
}
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//self.isReload = NO;
[self presentViewController:picker animated:YES completion:nil];
} else {
[self setAlertControllerWithTitle:@"提示" message:@"你没有相册" actionTitle:@"确定"];
}
}