前言
关于系统权限的获取,相信大家都不陌生,可是其中蕴含的知识确实不少。
怎样向用户索取权限是非常重要的。例如LBS类的应用,如果在索取权限时遭到用户的拒绝,那么该应用基本等同于无用了,更坏的是,点击“不允许”是很轻松的,而要撤销这个决定则不太容易,用户至少需要以下五步,一次性成功获取权限的重要性不言而喻,
关于这一点,好的设置可以概括为这样:
除非当前确实需要,否则不要向用户索取权限。
索取权限时要让用户明确的了解授权后的好处是什么。
联网权限
相册权限
相机、麦克风权限
定位权限
推送权限
通讯录权限
日历、备忘录权限
联网权限
引入头文件@import CoreTelephony;
应用启动后,检测应用中是否有联网权限
typedefNS_ENUM(NSUInteger,CTCellularDataRestrictedState) {
kCTCellularDataRestrictedStateUnknown,//权限未知
kCTCellularDataRestricted,//权限被关闭,
kCTCellularDataNotRestricted//权限开启
};
使用时需要注意的关键点:
CTCellularData只能检测蜂窝权限,不能检测WiFi权限。
一个CTCellularData实例新建时,restrictedState是kCTCellularDataRestrictedStateUnknown,
之后在cellularDataRestrictionDidUpdateNotifier里会有一次回调,此时才能获取到正确的权限状态。
当用户在设置里更改了app的权限时,cellularDataRestrictionDidUpdateNotifier会收到回调,如果要停止监听,
必须将cellularDataRestrictionDidUpdateNotifier设置为nil。
赋值给cellularDataRestrictionDidUpdateNotifier的block并不会自动释放,
即便你给一个局部变量的CTCellularData实例设置监听,当权限更改时,还是会收到回调,所以记得将block置nil。
CTCellularData*cellularData = [[CTCellularDataalloc]init];
cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedStatestate)
{//获取联网状态 switch (state)
{
casekCTCellularDataRestricted:NSLog(@"Restricrted");break;
casekCTCellularDataNotRestricted:NSLog(@"Not Restricted");break;
//未知,第一次请求
casekCTCellularDataRestrictedStateUnknown:NSLog(@"Unknown");break;
default:break;
};
};
查询应用是否有联网功能
CTCellularData*cellularData = [[CTCellularDataalloc]init];
CTCellularDataRestrictedStatestate = cellularData.restrictedState;
switch(state) {
casekCTCellularDataRestricted:NSLog(@"Restricrted");break;
casekCTCellularDataNotRestricted:NSLog(@"Not Restricted");break;
casekCTCellularDataRestrictedStateUnknown:NSLog(@"Unknown");break;default:break;
}
注意:当应用被设置为不联网,使用的时候,系统会自动弹出警告“xxxx 已被关闭网络”点击可以去设置,自动跳转到设置中心里。
iOS10 国行机第一次安装App时会有一个权限弹框弹出,在允许之前是没有网络的,网上对于现状已有描述和解决方法:
(1)在引导页中诱导出网络权限弹框,这样就不会影响到之后应用的网络请求。
(2)允许用户手动重新请求。出现数据空白时,如果在空白页面上有“重新加载”的按钮。
(3) 允许用户手动重新请求。出现数据空白时,如果在空白页面上有“重新加载”的按钮。
相册权限--iOS 9.0之前
导入头文件@import AssetsLibrary;
检查是否有相册权限
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
switch(status) {
caseALAuthorizationStatusAuthorized:NSLog(@"Authorized");break;
caseALAuthorizationStatusDenied:NSLog(@"Denied");break;
caseALAuthorizationStatusNotDetermined:NSLog(@"not Determined");break;
caseALAuthorizationStatusRestricted:NSLog(@"Restricted");break;default:break;
}
相册权限--iOS 8.0之后
导入头文件@import Photos;
检查是否有相册权限
PHAuthorizationStatus photoAuthorStatus = [PHPhotoLibrary authorizationStatus];
switch(photoAuthorStatus) {
casePHAuthorizationStatusAuthorized:NSLog(@"Authorized");break;
casePHAuthorizationStatusDenied:NSLog(@"Denied");break;
casePHAuthorizationStatusNotDetermined:NSLog(@"not Determined");break;
casePHAuthorizationStatusRestricted:NSLog(@"Restricted");break;default:break;}
获取相册权限
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if(status == PHAuthorizationStatusAuthorized)
{NSLog(@"Authorized"); }
else{NSLog(@"Denied or Restricted");
} }];
相机和麦克风权限
导入头文件@import AVFoundation;
检查是否有相机或麦克风权限
AVAuthorizationStatusAVstatus= [AVCaptureDeviceauthorizationStatusForMediaType:AVMediaTypeVideo];//相机权限
AVAuthorizationStatusAVstatus= [AVCaptureDeviceauthorizationStatusForMediaType:AVMediaTypeAudio];//麦克风权限
switch(AVstatus) {
//允许状态
caseAVAuthorizationStatusAuthorized:NSLog(@"Authorized");break;
//不允许状态,可以弹出一个alertview提示用户在隐私设置中开启权限
caseAVAuthorizationStatusDenied:NSLog(@"Denied");break;
//未知,第一次申请权限
caseAVAuthorizationStatusNotDetermined:NSLog(@"not Determined");break;
//此应用程序没有被授权访问,可能是家长控制权限
caseAVAuthorizationStatusRestricted:NSLog(@"Restricted");break;default:break;
}
获取相机或麦克风权限
[AVCaptureDevicerequestAccessForMediaType:
AVMediaTypeVideocompletionHandler:^(BOOLgranted) {//相机权限
if(granted) {NSLog(@"Authorized"); }
else{NSLog(@"Denied or Restricted"); }}];
[AVCaptureDevicerequestAccessForMediaType:
AVMediaTypeAudiocompletionHandler:^(BOOLgranted)
{//麦克风权限
if(granted) {NSLog(@"Authorized"); }
else{NSLog(@"Denied or Restricted");
}}];
定位权限
导入头文件@import CoreLocation;
由于iOS8.0之后定位方法的改变,需要在info.plist中进行配置;
配置文件
检查是否有定位权限
BOOLisLocation = [CLLocationManagerlocationServicesEnabled];
if(!isLocation) {NSLog(@"not turn on the location");}
CLAuthorizationStatusCLstatus= [CLLocationManagerauthorizationStatus];
switch(CLstatus) {
casekCLAuthorizationStatusAuthorizedAlways:NSLog(@"Always Authorized");break;
casekCLAuthorizationStatusAuthorizedWhenInUse:NSLog(@"AuthorizedWhenInUse");break;
casekCLAuthorizationStatusDenied:NSLog(@"Denied");break;
casekCLAuthorizationStatusNotDetermined:NSLog(@"not Determined");break;
casekCLAuthorizationStatusRestricted:NSLog(@"Restricted");break;default:break;
}
获取定位权限
这里有一个细节要注意,CLLocationManager实例必须是全局的变量,否则授权提示弹框会一闪而过,不会一直显示。
manager = [[CLLocationManageralloc] init];
manager.delegate=self;
[manager requestAlwaysAuthorization];//一直获取定位信息
[manager requestWhenInUseAuthorization];//使用的时候获取定位信息
在代理方法中查看权限是否改变
- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch(status) {
casekCLAuthorizationStatusAuthorizedAlways:NSLog(@"Always Authorized");break;
casekCLAuthorizationStatusAuthorizedWhenInUse:NSLog(@"AuthorizedWhenInUse");break;
casekCLAuthorizationStatusDenied:NSLog(@"Denied");break;
casekCLAuthorizationStatusNotDetermined:NSLog(@"not Determined");break;
casekCLAuthorizationStatusRestricted:NSLog(@"Restricted");break;default:break;
}}
推送权限
检查是否有通讯权限
UIUserNotificationSettings*settings = [[UIApplicationsharedApplication] currentUserNotificationSettings];
switch(settings.types) {
caseUIUserNotificationTypeNone:NSLog(@"None");break;
caseUIUserNotificationTypeAlert:NSLog(@"Alert Notification");break;
caseUIUserNotificationTypeBadge:NSLog(@"Badge Notification");break;
caseUIUserNotificationTypeSound:NSLog(@"sound Notification'");break;default:break;
}
获取推送权限
UIUserNotificationSettings*setting = [UIUserNotificationSettingssettingsForTypes:UIUserNotificationTypeSound|UIUserNotificationTypeAlert|UIUserNotificationTypeBadgecategories:nil];
[[UIApplicationsharedApplication] registerUserNotificationSettings:setting];
通讯录权限
iOS9.0之前
导入头文件@import AddressBook;
检查是否有通讯录权限
ABAuthorizationStatus ABstatus = ABAddressBookGetAuthorizationStatus();
switch(ABstatus) {
casekABAuthorizationStatusAuthorized:NSLog(@"Authorized");break;
casekABAuthorizationStatusDenied:NSLog(@"Denied'");break;
casekABAuthorizationStatusNotDetermined:NSLog(@"not Determined");break;
casekABAuthorizationStatusRestricted:NSLog(@"Restricted");break;default:break;
}
获取通讯录权限
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL,NULL);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(boolgranted,CFErrorReferror) {
if(granted)
{NSLog(@"Authorized");
CFRelease(addressBook);
}else{NSLog(@"Denied or Restricted");
}});
iOS9.0及以后
导入头文件 **@importContacts;**
检查是否有通讯录权限
CNAuthorizationStatusstatus = [CNContactStoreauthorizationStatusForEntityType:CNEntityTypeContacts];
switch(status) {
caseCNAuthorizationStatusAuthorized: {NSLog(@"Authorized:"); }break;
caseCNAuthorizationStatusDenied:{NSLog(@"Denied"); }break;
caseCNAuthorizationStatusRestricted:{NSLog(@"Restricted"); }break;
caseCNAuthorizationStatusNotDetermined:{NSLog(@"NotDetermined"); }break;
}
查询是否获取通讯录权限
CNContactStore*contactStore = [[CNContactStorealloc] init]; [contactStore requestAccessForEntityType:CNEntityTypeContactscompletionHandler:^(BOOLgranted,NSError* _Nullable error) {
if(granted) {NSLog(@"Authorized"); }
else{NSLog(@"Denied or Restricted"); }
}];
日历、备忘录权限
导入头文件
检查是否有日历或者备忘录权限
typedefNS_ENUM(NSUInteger, EKEntityType) { EKEntityTypeEvent,//日历 EKEntityTypeReminder //备忘 };
EKAuthorizationStatus EKstatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
switch(EKstatus) {
caseEKAuthorizationStatusAuthorized:NSLog(@"Authorized");break;
caseEKAuthorizationStatusDenied:NSLog(@"Denied'");break;
caseEKAuthorizationStatusNotDetermined:NSLog(@"not Determined");break;
caseEKAuthorizationStatusRestricted:NSLog(@"Restricted");
break;
default:break;
}
查询是否获取日历或备忘录权限
EKEventStore *store = [[EKEventStore alloc]init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOLgranted,NSError* _Nullable error) {
if(granted) {
NSLog(@"Authorized"); }
else{NSLog(@"Denied or Restricted"); }
}];
网友评论