在人们越来越注意隐私的今天,用户的app的权限的下放也越来越谨慎。
得到权限的过程,一般分为两种:1. 一次获取要用的所有权限; 2.用什么功能获取什么权限。当然两种方式各有有缺点,方法一,用户体验好,但是用户可能不完全明白权限的用途。方法二,也是现在大部分app的做法,使用过程中不断的弹框获取提示获取权限,不过这样用户也明白权限的用途。
下面列举一下主要的权限获取和检验方法(iOS8
以后的版本为例):
-
相册
//头文件 iOS8以后版本可用PhotoKit
#import <Photos/Photos.h>
检查是否有相册权限
PHAuthorizationStatus photoAuthStatus = [PHPhotoLibrary authorizationStatus];
switch (photoAuthStatus) {
case PHAuthorizationStatusNotDetermined:
NSLog(@"未询问用户是否授权");
break;
case PHAuthorizationStatusRestricted:
NSLog(@"未授权,例如家长控制");
break;
case PHAuthorizationStatusDenied:
NSLog(@"未授权,用户拒绝造成的");
break;
case PHAuthorizationStatusAuthorized:
NSLog(@"同意授权相册");
break;
default:
break;
}
获取相册权限
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
NSLog(@"用户同意授权相册");
}else {
NSLog(@"用户拒绝授权相册");
}
}];
-
相机和麦克风
//头文件
#import <AVFoundation/AVFoundation.h>
检查是否有相机权限或者麦克风权限
AVF_EXPORT NSString *const AVMediaTypeVideo NS_AVAILABLE(10_7, 4_0); //相机
AVF_EXPORT NSString *const AVMediaTypeAudio NS_AVAILABLE(10_7, 4_0);//麦克风
AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (videoAuthStatus) {
case AVAuthorizationStatusNotDetermined:
NSLog(@"未询问用户是否授权");
break;
case AVAuthorizationStatusRestricted:
NSLog(@"未授权,例如家长控制");
break;
case AVAuthorizationStatusDenied:
NSLog(@"未授权,用户拒绝造成的");
break;
case AVAuthorizationStatusAuthorized:
NSLog(@"同意授权相机");
break;
default:
break;
}
获取相机权限或者麦克风权限
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted){
NSLog(@"用户同意授权相机");
}else {
NSLog(@"用户拒绝授权相机");
}
}];
-
推送
检查是否有推送权限
UIUserNotificationSettings *notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
switch (notificationSettings.types) {
case UIUserNotificationTypeNone:
NSLog(@"没有推送权限");
break;
case UIUserNotificationTypeBadge:
NSLog(@"带角标的推送");
break;
case UIUserNotificationTypeSound:
NSLog(@"带声音的推送");
break;
case UIUserNotificationTypeAlert:
NSLog(@"带通知的推送");
break;
default:
break;
}
获取推送权限
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
-
通讯录
//头文件 iOS9之后苹果推荐使用<Contacts/Contacts.h>
#import <AddressBook/AddressBook.h>
检查是否有通讯录权限
ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus();
switch (authorizationStatus) {
case kABAuthorizationStatusNotDetermined:
NSLog(@"未询问用户是否授权");
break;
case kABAuthorizationStatusRestricted:
NSLog(@"未授权,例如家长控制");
break;
case kABAuthorizationStatusDenied:
NSLog(@"未授权,用户拒绝造成的");
break;
case kABAuthorizationStatusAuthorized:
NSLog(@"同意授权通讯录");
break;
default:
break;
}
获取通讯录权限
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) {
NSLog(@"用户同意授权通讯录");
CFRelease(addressBook);
} else {
NSLog(@"用户拒绝授权通讯录");
}
});
- 日历和备忘录
//头文件
#import <EventKit/EventKit.h>
检查是否有日历权限或者备忘录权限
typedef NS_ENUM(NSUInteger, EKEntityType) {
EKEntityTypeEvent,//日历
EKEntityTypeReminder//备忘录
};
EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
switch (status) {
case EKAuthorizationStatusNotDetermined:
NSLog(@"未询问用户是否授权");
break;
case EKAuthorizationStatusRestricted:
NSLog(@"未授权,例如家长控制");
break;
case EKAuthorizationStatusDenied:
NSLog(@"未授权,用户拒绝造成的");
break;
case EKAuthorizationStatusAuthorized:
NSLog(@"同意授权日历");
break;
default:
break;
}
获取日历权限或者备忘录权限
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent
completion:^(BOOL granted, NSError *error) {
if (error) {
NSLog(@"日历出现了错误");
return;
}
if (granted) {
NSLog(@"用户同意授权日历");
} else {
NSLog(@"用户拒绝授权日历");
}
}];
-
定位
//头文件
#import <CoreLocation/CoreLocation.h>
由于iOS8之后定位方法的改变,需要现在info.plist中进行配置
info.png检查是否有定位权限
BOOL isLocation = [CLLocationManager locationServicesEnabled]; //是否开启定位服务
if (!isLocation) {
NSLog(@"用户未开启定位");
return ;
}
CLAuthorizationStatus locationStatus = [CLLocationManager authorizationStatus];
switch (locationStatus) {
case kCLAuthorizationStatusNotDetermined:
NSLog(@"未询问用户是否授权");
break;
case kCLAuthorizationStatusRestricted:
NSLog(@"未授权,例如家长控制");
break;
case kCLAuthorizationStatusDenied:
NSLog(@"未授权,用户拒绝造成的");
break;
case kCLAuthorizationStatusAuthorizedAlways:
NSLog(@"同意授权一直获取定位信息");
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"同意授权在使用时获取定位信息");
break;
default:
break;
}
获取定位权限
CLLocationManager *manager = [[CLLocationManager alloc] init];
[manager requestAlwaysAuthorization];//一直获取定位信息
[manager requestWhenInUseAuthorization];//使用时获取定位信息
定位中还有一个代理方法,可以查看权限改变
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
switch (status) {
case kCLAuthorizationStatusNotDetermined:
NSLog(@"未询问用户是否授权");
break;
case kCLAuthorizationStatusRestricted:
NSLog(@"未授权,例如家长控制");
break;
case kCLAuthorizationStatusDenied:
NSLog(@"未授权,用户拒绝造成的");
break;
case kCLAuthorizationStatusAuthorizedAlways:
NSLog(@"同意授权一直获取定位信息");
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"同意授权在使用时获取定位信息");
break;
default:
break;
}
}
所有获取权限的方法用户进行了第一次操作之后,都会没有用了,iOS8之后苹果把这些设置都整合在了一起,通过下面这个方法则可以打开。
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
最后这是一个非常优秀的权限管理库PermissionScope
本文的Demo
网友评论