1.判断用户是否有权限访问相册
#import <AssetsLibrary/AssetsLibrary.h>
ALAuthorizationStatus author =[ALAssetsLibrary authorizationStatus];
if (author == kCLAuthorizationStatusRestricted || author ==kCLAuthorizationStatusDenied)
{
//无权限
}
可以注意一下下面这个
typedef enum {
kCLAuthorizationStatusNotDetermined = 0, // 用户尚未做出选择这个应用程序的问候
kCLAuthorizationStatusRestricted, // 此应用程序没有被授权访问的照片数据。可能是家长控制权限
kCLAuthorizationStatusDenied, // 用户已经明确否认了这一照片数据的应用程序访问
kCLAuthorizationStatusAuthorized // 用户已经授权应用访问照片数据
} CLAuthorizationStatus;
iOS 8 之后推荐用 #import <Photos/Photos.h> 中的判断
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusRestricted ||
status == PHAuthorizationStatusDenied) {
return NO;
}
return YES;
typedef NS_ENUM(NSInteger, PHAuthorizationStatus) {
PHAuthorizationStatusNotDetermined = 0, // 默认还没做出选择
PHAuthorizationStatusRestricted, // 此应用程序没有被授权访问的照片数据
PHAuthorizationStatusDenied, // 用户已经明确否认了这一照片数据的应用程序访问
PHAuthorizationStatusAuthorized // 用户已经授权应用访问照片数据
} NS_AVAILABLE_IOS(8_0);
推荐使用
- (BOOL)isCanUsePhotos {
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
ALAuthorizationStatus author =[ALAssetsLibrary authorizationStatus];
if (author == kCLAuthorizationStatusRestricted || author == kCLAuthorizationStatusDenied) {
//无权限
return NO;
}
}
else {
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusRestricted ||
status == PHAuthorizationStatusDenied) {
//无权限
return NO;
}
}
return YES;
}
英语环境下的没有权限但实际上系统会提示下面一张图,可以不做判断的,哈哈哈
2.判断用户是否有权限访问相机
iOS7之前都可以访问相机,iOS7之后访问相机有权限设置,所以还是需要我们手动去设置判断。
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
{
//无权限
}
typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {
AVAuthorizationStatusNotDetermined = 0,// 系统还未知是否访问,第一次开启相机时
AVAuthorizationStatusRestricted, // 受限制的
AVAuthorizationStatusDenied, //不允许
AVAuthorizationStatusAuthorized // 允许状态
} NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
网友评论
//无权限
return NO;
}
这里应该用,上面是CoreLocation的:
if (author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied) {
//无权限
return NO;
}
对的,那个用的是CoreLocation 中的,确实在9.0之前应该按上述你说的那样使用。
不过 ALAuthorizationStatusRestricted 也可以换成PHAuthorizationStatusRestricted,毕竟这个在 9.0 以后也被弃用啦