前言
关于系统权限的获取,相信大家都不陌生,可是其中蕴含的知识确实不少。
怎样向用户索取权限是非常重要的。例如LBS类的应用,如果在索取权限时遭到用户的拒绝,那么该应用基本等同于无用了,更坏的是,点击“不允许”是很轻松的,而要撤销这个决定则不太容易,用户至少需要以下五步,一次性成功获取权限的重要性不言而喻。
关于这一点,好的设置可以概括为这样:
除非当前确实需要,否则不要向用户索取权限。
索取权限时要让用户明确的了解授权后的好处是什么。
权限分类
联网权限
相册权限
相机、麦克风权限
定位权限
推送权限
通讯录权限
日历、备忘录权限
联网权限
引入头文件 @import CoreTelephony;
应用启动后,检测应用中是否有联网权限
typedef NS_ENUM(NSUInteger, CTCellularDataRestrictedState) {
kCTCellularDataRestrictedStateUnknown,//权限未知
kCTCellularDataRestricted,//权限被关闭,
kCTCellularDataNotRestricted//权限开启
};
使用时需要注意的关键点:
CTCellularData 只能检测蜂窝权限,不能检测WiFi权限。
一个CTCellularData实例新建时,restrictedState是kCTCellularDataRestrictedStateUnknown,
之后在cellularDataRestrictionDidUpdateNotifier里会有一次回调,此时才能获取到正确的权限状态。
当用户在设置里更改了app的权限时,cellularDataRestrictionDidUpdateNotifier会收到回调,如果要停止监听,
必须将cellularDataRestrictionDidUpdateNotifier设置为nil。
赋值给cellularDataRestrictionDidUpdateNotifier的block并不会自动释放,
即便你给一个局部变量的CTCellularData实例设置监听,当权限更改时,还是会收到回调,所以记得将block置nil。
CTCellularData *cellularData = [[CTCellularData alloc]init];
cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state)
{ //获取联网状态 switch (state)
{
case kCTCellularDataRestricted: NSLog(@"Restricrted"); break;
case kCTCellularDataNotRestricted: NSLog(@"Not Restricted"); break;
//未知,第一次请求
case kCTCellularDataRestrictedStateUnknown: NSLog(@"Unknown"); break;
default: break;
};
};
查询应用是否有联网功能
CTCellularData *cellularData = [[CTCellularData alloc]init];
CTCellularDataRestrictedState state = cellularData.restrictedState;
switch (state) {
case kCTCellularDataRestricted: NSLog(@"Restricrted"); break;
case kCTCellularDataNotRestricted: NSLog(@"Not Restricted"); break;
case kCTCellularDataRestrictedStateUnknown: NSLog(@"Unknown"); break; default: break;
}
注意:当应用被设置为不联网,使用的时候,系统会自动弹出警告“xxxx 已被关闭网络”点击可以去设置,自动跳转到设置中心里。
iOS10 国行机第一次安装App时会有一个权限弹框弹出,在允许之前是没有网络的,网上对于现状已有描述和解决方法:
(1)在引导页中诱导出网络权限弹框,这样就不会影响到之后应用的网络请求。
(2)允许用户手动重新请求。出现数据空白时,如果在空白页面上有“重新加载”的按钮。
(3) 允许用户手动重新请求。出现数据空白时,如果在空白页面上有“重新加载”的按钮。
相册权限--iOS 9.0之前
导入头文件@import AssetsLibrary;
检查是否有相册权限
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
switch (status) {
case ALAuthorizationStatusAuthorized: NSLog(@"Authorized"); break;
case ALAuthorizationStatusDenied: NSLog(@"Denied"); break;
case ALAuthorizationStatusNotDetermined: NSLog(@"not Determined"); break;
case ALAuthorizationStatusRestricted: NSLog(@"Restricted"); break; default: break;
}
相册权限--iOS 8.0之后
导入头文件@import Photos;
检查是否有相册权限
PHAuthorizationStatus photoAuthorStatus = [PHPhotoLibrary authorizationStatus];
switch (photoAuthorStatus) {
case PHAuthorizationStatusAuthorized: NSLog(@"Authorized"); break;
case PHAuthorizationStatusDenied: NSLog(@"Denied"); break;
case PHAuthorizationStatusNotDetermined: NSLog(@"not Determined"); break;
case PHAuthorizationStatusRestricted: NSLog(@"Restricted"); break; default: break;}
获取相册权限
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized)
{ NSLog(@"Authorized"); }
else{ NSLog(@"Denied or Restricted");
} }];
相机和麦克风权限
导入头文件@import AVFoundation;
检查是否有相机或麦克风权限
AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];//相机权限
AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//麦克风权限
switch (AVstatus) {
//允许状态
case AVAuthorizationStatusAuthorized: NSLog(@"Authorized"); break;
//不允许状态,可以弹出一个alertview提示用户在隐私设置中开启权限
case AVAuthorizationStatusDenied: NSLog(@"Denied"); break;
//未知,第一次申请权限
case AVAuthorizationStatusNotDetermined: NSLog(@"not Determined"); break;
//此应用程序没有被授权访问,可能是家长控制权限
case AVAuthorizationStatusRestricted: NSLog(@"Restricted"); break; default: break;
}
获取相机或麦克风权限
[AVCaptureDevice requestAccessForMediaType:
AVMediaTypeVideo completionHandler:^(BOOL granted) {//相机权限
if (granted) { NSLog(@"Authorized"); }
else{ NSLog(@"Denied or Restricted"); }}];
[AVCaptureDevice requestAccessForMediaType:
AVMediaTypeAudio completionHandler:^(BOOL granted)
{//麦克风权限
if (granted) { NSLog(@"Authorized"); }
else{ NSLog(@"Denied or Restricted");
}}];
定位权限
导入头文件@import CoreLocation;
由于iOS8.0之后定位方法的改变,需要在info.plist中进行配置;
配置文件
检查是否有定位权限
BOOL isLocation = [CLLocationManager locationServicesEnabled];
if (!isLocation) { NSLog(@"not turn on the location");}
CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus];
switch (CLstatus) {
case kCLAuthorizationStatusAuthorizedAlways: NSLog(@"Always Authorized"); break;
case kCLAuthorizationStatusAuthorizedWhenInUse: NSLog(@"AuthorizedWhenInUse"); break;
case kCLAuthorizationStatusDenied: NSLog(@"Denied"); break;
case kCLAuthorizationStatusNotDetermined: NSLog(@"not Determined"); break;
case kCLAuthorizationStatusRestricted: NSLog(@"Restricted"); break; default: break;
}
获取定位权限
这里有一个细节要注意, CLLocationManager 实例必须是全局的变量,否则授权提示弹框会一闪而过,不会一直显示。
manager = [[CLLocationManager alloc] init];
manager.delegate= self;
[manager requestAlwaysAuthorization];//一直获取定位信息
[manager requestWhenInUseAuthorization];//使用的时候获取定位信息
在代理方法中查看权限是否改变
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusAuthorizedAlways: NSLog(@"Always Authorized"); break;
case kCLAuthorizationStatusAuthorizedWhenInUse: NSLog(@"AuthorizedWhenInUse"); break;
case kCLAuthorizationStatusDenied: NSLog(@"Denied"); break;
case kCLAuthorizationStatusNotDetermined: NSLog(@"not Determined"); break;
case kCLAuthorizationStatusRestricted: NSLog(@"Restricted"); break; default: break;
}}
推送权限
检查是否有通讯权限
UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
switch (settings.types) {
case UIUserNotificationTypeNone: NSLog(@"None"); break;
case UIUserNotificationTypeAlert: NSLog(@"Alert Notification"); break;
case UIUserNotificationTypeBadge: NSLog(@"Badge Notification"); break;
case UIUserNotificationTypeSound: NSLog(@"sound Notification'"); break; default: break;
}
获取推送权限
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];
iOS应用内跳转系统推送设置页
通讯录权限
iOS9.0之前
导入头文件 @import AddressBook;
检查是否有通讯录权限
ABAuthorizationStatus ABstatus = ABAddressBookGetAuthorizationStatus();
switch (ABstatus) {
case kABAuthorizationStatusAuthorized: NSLog(@"Authorized"); break;
case kABAuthorizationStatusDenied: NSLog(@"Denied'"); break;
case kABAuthorizationStatusNotDetermined: NSLog(@"not Determined"); break;
case kABAuthorizationStatusRestricted: NSLog(@"Restricted"); break; default: break;
}
获取通讯录权限
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted)
{ NSLog(@"Authorized");
CFRelease(addressBook);
}else{ NSLog(@"Denied or Restricted");
}});
iOS9.0及以后
导入头文件 **@import Contacts;**
检查是否有通讯录权限
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
switch (status) {
case CNAuthorizationStatusAuthorized: { NSLog(@"Authorized:"); } break;
case CNAuthorizationStatusDenied:{ NSLog(@"Denied"); } break;
case CNAuthorizationStatusRestricted:{ NSLog(@"Restricted"); } break;
case CNAuthorizationStatusNotDetermined:{ NSLog(@"NotDetermined"); } break;
}
查询是否获取通讯录权限
CNContactStore *contactStore = [[CNContactStore alloc] init]; [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) { NSLog(@"Authorized"); }
else{ NSLog(@"Denied or Restricted"); }
}];
日历、备忘录权限
导入头文件
检查是否有日历或者备忘录权限
typedef NS_ENUM(NSUInteger, EKEntityType) { EKEntityTypeEvent,//日历 EKEntityTypeReminder //备忘 };
EKAuthorizationStatus EKstatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
switch (EKstatus) {
case EKAuthorizationStatusAuthorized: NSLog(@"Authorized"); break;
case EKAuthorizationStatusDenied: NSLog(@"Denied'"); break;
case EKAuthorizationStatusNotDetermined: NSLog(@"not Determined"); break;
case EKAuthorizationStatusRestricted: NSLog(@"Restricted");
break;
default: break;
}
查询是否获取日历或备忘录权限
EKEventStore *store = [[EKEventStore alloc]init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"Authorized"); }
else{ NSLog(@"Denied or Restricted"); }
}];