苹果公司非常注重用户的隐私问题,所以在使用手机上一些涉及隐私的功能时,比如相册,相机,通讯录,录音权限等,需要征求用户的许可才可以使用。
在使用相册,相机,通讯录,录音等功能时,我们需要通过不同的API获取到某一项功能的用户授权权限,用户授权的权限类型总结起来都可以归结为以下几种:
AuthorizationStatusNotDetermined // 用户从未进行过授权等处理,首次访问相应内容会提示用户进行授权AuthorizationStatusAuthorized = 0, // 用户已授权,允许访问AuthorizationStatusDenied, // 用户拒绝访问AuthorizationStatusRestricted, //用户无法改编自合格权限,比如家长控制
相册权限获取分三步:
设备是否支持
在获取相册的权限之前,先要判断设备是否支持获取媒体资源,比如我想要挑选照片,但是现在相册中照片数量为0,那这时候就不能支持相册了:
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]typedef enum UIImagePickerControllerSourceType : NSInteger { UIImagePickerControllerSourceTypePhotoLibrary, //相册 UIImagePickerControllerSourceTypeCamera, //相机 UIImagePickerControllerSourceTypeSavedPhotosAlbum} UIImagePickerControllerSourceType;
获取相册权限状态
如果第一步返回YES,也就是设备支持,那就可以获取目前的权限状态了:
PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
权限状态有下列四种:
typedef enum PHAuthorizationStatus : NSInteger { PHAuthorizationStatusNotDetermined = 0, PHAuthorizationStatusRestricted, PHAuthorizationStatusDenied, PHAuthorizationStatusAuthorized } PHAuthorizationStatus;
请求权限
第二步中若status为PHAuthorizationStatusNotDetermined,也就是还没有请求过权限,那么就要请求权限:
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { }];
和相册权限一样,分三步:
设备是否支持:
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]
获取相机权限状态
如果第一步返回YES,那么就可以获取目前的权限状态:
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
权限状态有下列四种:
typedef enum AVAuthorizationStatus : NSInteger { AVAuthorizationStatusNotDetermined = 0, AVAuthorizationStatusRestricted = 1, AVAuthorizationStatusDenied = 2, AVAuthorizationStatusAuthorized = 3} AVAuthorizationStatus;
请求权限
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { }];
获取通讯录权限状态:
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
权限状态有四种:
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]typedef enum UIImagePickerControllerSourceType : NSInteger { UIImagePickerControllerSourceTypePhotoLibrary, //相册 UIImagePickerControllerSourceTypeCamera, //相机 UIImagePickerControllerSourceTypeSavedPhotosAlbum} UIImagePickerControllerSourceType;
0
请求权限
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]typedef enum UIImagePickerControllerSourceType : NSInteger { UIImagePickerControllerSourceTypePhotoLibrary, //相册 UIImagePickerControllerSourceTypeCamera, //相机 UIImagePickerControllerSourceTypeSavedPhotosAlbum} UIImagePickerControllerSourceType;
1
获取录音权限状态:
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]typedef enum UIImagePickerControllerSourceType : NSInteger { UIImagePickerControllerSourceTypePhotoLibrary, //相册 UIImagePickerControllerSourceTypeCamera, //相机 UIImagePickerControllerSourceTypeSavedPhotosAlbum} UIImagePickerControllerSourceType;
2
权状态有三种:
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]typedef enum UIImagePickerControllerSourceType : NSInteger { UIImagePickerControllerSourceTypePhotoLibrary, //相册 UIImagePickerControllerSourceTypeCamera, //相机 UIImagePickerControllerSourceTypeSavedPhotosAlbum} UIImagePickerControllerSourceType;
3
请求权限
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]typedef enum UIImagePickerControllerSourceType : NSInteger { UIImagePickerControllerSourceTypePhotoLibrary, //相册 UIImagePickerControllerSourceTypeCamera, //相机 UIImagePickerControllerSourceTypeSavedPhotosAlbum} UIImagePickerControllerSourceType;
4
用户拒绝授权后,再下次要使用相应的功能,可能会忘记自己之前已经拒绝了该授权,从而可能获取不到数据,这个时候应该给用户一个提示,提示用户去设置里面更改权限。
这里的给出了一种做法就是直接跳转到该应用的设置页面:
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]typedef enum UIImagePickerControllerSourceType : NSInteger { UIImagePickerControllerSourceTypePhotoLibrary, //相册 UIImagePickerControllerSourceTypeCamera, //相机 UIImagePickerControllerSourceTypeSavedPhotosAlbum} UIImagePickerControllerSourceType;
5
LMAuthorizationTool.h:
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]typedef enum UIImagePickerControllerSourceType : NSInteger { UIImagePickerControllerSourceTypePhotoLibrary, //相册 UIImagePickerControllerSourceTypeCamera, //相机 UIImagePickerControllerSourceTypeSavedPhotosAlbum} UIImagePickerControllerSourceType;
6
LMAuthorizationTool.m
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]typedef enum UIImagePickerControllerSourceType : NSInteger { UIImagePickerControllerSourceTypePhotoLibrary, //相册 UIImagePickerControllerSourceTypeCamera, //相机 UIImagePickerControllerSourceTypeSavedPhotosAlbum} UIImagePickerControllerSourceType;
7
本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/iosxiangcexiangjitongxunluluyinquanxianchuli-2.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:1月 22, 2019 at 06:12 下午