最近做项目遇到了一些问题,就搜集了一些资料,集中在了一起
感谢:
CSDN 博主 [小手一背爱谁谁]
http://blog.csdn.net/saw471/article/details/52679746
wentian的博客
http://www.2cto.com/kf/201608/532614.html
本人只是搬运工,如有冒犯,请见谅!
一、权限状态说明
相册、相机、通讯录等授权状态目前都有种,都可以对应以下几种状态:
AuthorizationStatusNotDetermined // 用户从未进行过授权等处理,首次访问相应内容会提示用户进行授权
AuthorizationStatusAuthorized = 0, // 用户已授权,允许访问
AuthorizationStatusDenied, // 用户拒绝访问
AuthorizationStatusRestricted, // 应用没有相关权限,且当前用户无法改变这个权限,比如:家长控制
二、权限获取(以下都是iOS8之后的)
1.相册权限
是否支持
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]
获取权限状态
PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
请求权限
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
}];
权限状态
typedef NS_ENUM(NSInteger, PHAuthorizationStatus) {
PHAuthorizationStatusNotDetermined = 0, // User has not yet made a choice with regards to this application
PHAuthorizationStatusRestricted, // This application is not authorized to access photo data.
// The user cannot change this application’s status, possibly due to active restrictions
// such as parental controls being in place.
PHAuthorizationStatusDenied, // User has explicitly denied this application access to photos data.
PHAuthorizationStatusAuthorized // User has authorized this application to access photos data.
} NS_AVAILABLE_IOS(8_0);
2.拍照权限
是否支持
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]
获取权限状态
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
请求权限
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
}];
权限状态
typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {
AVAuthorizationStatusNotDetermined = 0,
AVAuthorizationStatusRestricted,
AVAuthorizationStatusDenied,
AVAuthorizationStatusAuthorized
} NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
三、拒绝授权的处理
用户拒绝授权后,如果访问相应内容可能会出现一些类似没有数据的情况,此时应该给用户提示,引导用户授权。
跳转到应用设置:
NSURL *settingUrl = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:settingUrl]) {
[[UIApplication sharedApplication] openURL:settingUrl];
}
四、info.plist 里面的相关配置
NSPhotoLibraryUsageDescription
App需要您的同意,才能访问相册
NSCameraUsageDescription
App需要您的同意,才能访问相机
NSMicrophoneUsageDescription
App需要您的同意,才能访问麦克风
NSLocationUsageDescription
App需要您的同意,才能访问位置
NSLocationWhenInUseUsageDescription
App需要您的同意,才能在使用期间访问位置
NSLocationAlwaysUsageDescription
App需要您的同意,才能始终访问位置
NSCalendarsUsageDescription
App需要您的同意,才能访问日历
NSRemindersUsageDescription
App需要您的同意,才能访问提醒事项
NSMotionUsageDescription App需要您的同意,才能访问运动与健身
NSHealthUpdateUsageDescription
App需要您的同意,才能访问健康更新
NSHealthShareUsageDescription
App需要您的同意,才能访问健康分享
NSBluetoothPeripheralUsageDescription
App需要您的同意,才能访问蓝牙
NSAppleMusicUsageDescription App需要您的同意,才能访问媒体资料库
*有个封装的工具类,可使用WTAuthorizationTool,里面还有通讯录的权限
pod "WTAuthorizationTool"