a、如何提高APP获取iOS系统权限的通过率呢?有以下几种方式:
1.在用户打开APP时就向用户请求权限;
2.告知用户授权权限后能够获得好处之后,再向用户请求权限;
3.在绝对必要的情况下才向用户请求权限,例如:用户访问照片库时请求访问系统相册权限;
4.在展示系统权限的对话框前,先向用户显示自定义的对话框,若用户选择不允许,默认无操作,若用户选择允许,再展示系统对话框。
b、权限分类:
1.联网权限
导入头文件#import <CoreTelephony/CTCellularData.h>
//应用启动后,检测应用中是否有联网权限
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;
};
};
//查询应用是否有联网功能
CTCellularDataRestrictedState state = cellularData.restrictedState;
switch (state) {
case kCTCellularDataRestricted:
NSLog(@"Restricrted");
break;
case kCTCellularDataNotRestricted:
NSLog(@"Not Restricted");
break;
case kCTCellularDataRestrictedStateUnknown:
NSLog(@"Unknown");
break;
default:
break;
}
2.相册权限
导入头文件#import <Photos/Photos.h>
//检查是否有相册权限
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");
}
}];
3.相机、麦克风权限
导入头文件#import <AVFoundation/AVFoundation.h>
//检查是否有相机或麦克风权限
AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];//相机权限
AVAuthorizationStatus AVstatus2 = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//麦克风权限
switch (AVstatus | AVstatus2) {
case AVAuthorizationStatusAuthorized:
NSLog(@"Authorized");//经授权的
break;
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");
}
}];
4.定位权限
导入头文件#import <CoreLocation/CoreLocation.h>
//由于iOS8.0之后定位方法的改变,需要在info.plist中进行配置;
//Privacy - Location When In Use Usage Description 我们需要通过您的地理位置信息获取您周边的相关数据
//Privacy - Location Always Usage Description 我们需要通过您的地理位置信息获取您周边的相关数据
//检查是否有定位权限
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 requestAlwaysAuthorization];//一直获取定位信息
[manager requestWhenInUseAuthorization];//使用的时候获取定位信息
5.推送通知权限
导入头文件#import <UserNotifications/UserNotifications.h>
//检查是否有推送通知权限
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];
6.通讯录权限
导入头文件#import <AddressBook/AddressBook.h>
导入头文件#import <Contacts/Contacts.h>
/**
iOS9之前的通讯录框架:
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
iOS9之后的通讯录框架:
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
*/
//检查是否有通讯录权限
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之后的通讯录框架
CNAuthorizationStatus CNstatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
switch (CNstatus) {
case CNAuthorizationStatusAuthorized:
NSLog(@"Authorized");
break;
case CNAuthorizationStatusDenied:
NSLog(@"Denied");
break;
case CNAuthorizationStatusNotDetermined:
NSLog(@"not Determined");
break;
case CNAuthorizationStatusRestricted:
NSLog(@"Restricted");
break;
default:
break;
}
CNContactStore *contactStore = [[CNContactStore alloc] init];
[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"Authorized");
}else{
NSLog(@"Denied or Restricted");
}
}];
7.日历、备忘录权限
导入头文件#import <EventKit/EventKit.h>
/**
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");
}
}];
c、用户拒绝授权后,如果访问相应内容可能会出现一些类似没有数据的情况,此时应该给用户提示,引导用户授权。比如跳转到应用设置:
NSURL *settingUrl = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:settingUrl]) {
[[UIApplication sharedApplication] openURL:settingUrl];
}