下面是自己项目中写的请求相机跟相册权限,有需要则直接copy过来用
需要#import <Photos/Photos.h>
相册权限
- (void)requestCameraPermissions {
//相机权限
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus ==AVAuthorizationStatusRestricted ||//此应用程序没有被授权访问的照片数据。
authStatus ==AVAuthorizationStatusDenied) //用户已经明确否认了这一照片数据的应用程序访问
{
//弹窗
UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"未得到您的允许,无法打开相机进行拍照" message:nil preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alertC animated:YES completion:nil];
UIAlertAction * action = [UIAlertAction actionWithTitle:@"去打开" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
// 无权限 引导去开启
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication]openURL:url];
}
}];
action.textColor = NAV_Login_BGC;
[alertC addAction:action];
}else{
[self openCamera]; //打开相机
}
}
- (void)requestPhoto{
// 判断授权状态
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusRestricted) { // 此应用程序没有被授权访问的照片数据。可能是家长控制权限。
NSLog(@"因为系统原因, 无法访问相册");
} else if (status == PHAuthorizationStatusDenied) { // 用户拒绝访问相册
//弹窗
UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"未得到您的允许,读取您的相册" message:nil preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alertC animated:YES completion:nil];
UIAlertAction * action = [UIAlertAction actionWithTitle:@"去打开" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
// 无权限 引导去开启
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}];
action.textColor = NAV_Login_BGC;
[alertC addAction:action];
} else if (status == PHAuthorizationStatusAuthorized) { // 用户允许访问相册
// 放一些使用相册的代码
[self openAlbum];
} else if (status == PHAuthorizationStatusNotDetermined) { // 用户还没有做出选择
// 弹框请求用户授权
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) { // 用户点击了好
// 放一些使用相册的代码
[self openAlbum];
}
}];
}
}