iOS开发中的这些权限,你搞懂了吗?

移动开发
APP开发避免不开系统权限的问题,如何在APP以更加友好的方式向用户展示系统权限,似乎也是开发过程中值得深思的一件事;那如何提高APP获取iOS系统权限的通过率呢?

写在前面

APP开发避免不开系统权限的问题,如何在APP以更加友好的方式向用户展示系统权限,似乎也是开发过程中值得深思的一件事;

iOS开发中的这些权限,你搞懂了吗?

那如何提高APP获取iOS系统权限的通过率呢?有以下几种方式:

  • 在用户打开APP时就向用户请求权限;
  • 告知用户授权权限后能够获得好处之后,再向用户请求权限;
  • 在绝对必要的情况下才向用户请求权限,例如:用户访问照片库时请求访问系统相册权限;
  • 在展示系统权限的对话框前,先向用户显示自定义的对话框,若用户选择不允许,默认无操作,若用户选择允许,再展示系统对话框。

上述情况在开发过程中是经常遇到的,不同方式的选择会影响最后用户交互体验。这一点感悟正是源于上一周工作遇到的问题:适配iOS10,如何获取应用联网权限用以管理系统对话框的显示管理。当我把这个问题解决后,感觉有必要将常用的iOS系统权限做一个总结,以便后用。

权限分类

  • 联网权限
  • 相册权限
  • 相机、麦克风权限
  • 定位权限
  • 推送权限
  • 通讯录权限
  • 日历、备忘录权限

联网权限

引入头文件 @import CoreTelephony;

应用启动后,检测应用中是否有联网权限

  1. CTCellularData *cellularData = [[CTCellularData alloc]init]; 
  2. cellularData.cellularDataRestrictionDidUpdateNotifier =  ^(CTCellularDataRestrictedState state){ 
  3.   //获取联网状态 
  4.   switch (state) { 
  5.       case kCTCellularDataRestricted: 
  6.           NSLog(@"Restricrted"); 
  7.           break; 
  8.       case kCTCellularDataNotRestricted: 
  9.           NSLog(@"Not Restricted"); 
  10.           break; 
  11.       case kCTCellularDataRestrictedStateUnknown: 
  12.           NSLog(@"Unknown"); 
  13.           break; 
  14.       default
  15.           break; 
  16.   }; 
  17. }; 

查询应用是否有联网功能

  1. CTCellularData *cellularData = [[CTCellularData alloc]init]; 
  2. CTCellularDataRestrictedState state = cellularData.restrictedState; 
  3.  switch (state) { 
  4.   case kCTCellularDataRestricted: 
  5.       NSLog(@"Restricrted"); 
  6.       break; 
  7.   case kCTCellularDataNotRestricted: 
  8.       NSLog(@"Not Restricted"); 
  9.       break; 
  10.   case kCTCellularDataRestrictedStateUnknown: 
  11.       NSLog(@"Unknown"); 
  12.       break; 
  13.   default
  14.       break; 

相册权限--IOS 9.0之前

导入头文件@import AssetsLibrary;

检查是否有相册权限

  1. ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; 
  2. switch (status) { 
  3.   case ALAuthorizationStatusAuthorized: 
  4.       NSLog(@"Authorized"); 
  5.       break; 
  6.   case ALAuthorizationStatusDenied: 
  7.       NSLog(@"Denied"); 
  8.       break; 
  9.   case ALAuthorizationStatusNotDetermined: 
  10.       NSLog(@"not Determined"); 
  11.       break; 
  12.   case ALAuthorizationStatusRestricted: 
  13.       NSLog(@"Restricted"); 
  14.       break; 
  15.  
  16.   default
  17.       break; 

相册权限--IOS 8.0之后

导入头文件@import Photos;

检查是否有相册权限

  1. PHAuthorizationStatus photoAuthorStatus = [PHPhotoLibrary authorizationStatus]; 
  2. switch (photoAuthorStatus) { 
  3.   case PHAuthorizationStatusAuthorized: 
  4.       NSLog(@"Authorized"); 
  5.       break; 
  6.   case PHAuthorizationStatusDenied: 
  7.       NSLog(@"Denied"); 
  8.       break; 
  9.   case PHAuthorizationStatusNotDetermined: 
  10.       NSLog(@"not Determined"); 
  11.       break; 
  12.   case PHAuthorizationStatusRestricted: 
  13.       NSLog(@"Restricted"); 
  14.       break; 
  15.   default
  16.       break; 

![Uploading 144446-b8aca7ba38c5f8c0_695906.png . . .]获取相册权限

  1. [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { 
  2.     if (status == PHAuthorizationStatusAuthorized) { 
  3.         NSLog(@"Authorized"); 
  4.     }else
  5.         NSLog(@"Denied or Restricted"); 
  6.     } 
  7.     }]; 

相机和麦克风权限

导入头文件@import AVFoundation;

检查是否有相机或麦克风权限

  1. AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];//相机权限 
  2. AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//麦克风权限 
  3.  
  4. switch (AVstatus) { 
  5.   case AVAuthorizationStatusAuthorized: 
  6.       NSLog(@"Authorized"); 
  7.       break; 
  8.   case AVAuthorizationStatusDenied: 
  9.       NSLog(@"Denied"); 
  10.       break; 
  11.   case AVAuthorizationStatusNotDetermined: 
  12.       NSLog(@"not Determined"); 
  13.       break; 
  14.   case AVAuthorizationStatusRestricted: 
  15.       NSLog(@"Restricted"); 
  16.       break; 
  17.   default
  18.       break; 

获取相机或麦克风权限

  1. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {//相机权限 
  2.   if (granted) { 
  3.       NSLog(@"Authorized"); 
  4.   }else
  5.       NSLog(@"Denied or Restricted"); 
  6.   } 
  7. }]; 
  8.  
  9. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {//麦克风权限 
  10.   if (granted) { 
  11.       NSLog(@"Authorized"); 
  12.   }else
  13.       NSLog(@"Denied or Restricted"); 
  14.   } 
  15. }]; 

定位权限

导入头文件@import CoreLocation;

由于iOS8.0之后定位方法的改变,需要在info.plist中进行配置; 

iOS开发中的这些权限,你搞懂了吗?

检查是否有定位权限

  1. BOOL isLocation = [CLLocationManager locationServicesEnabled]; 
  2. if (!isLocation) { 
  3.   NSLog(@"not turn on the location"); 
  4. CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus]; 
  5. switch (CLstatus) { 
  6.   case kCLAuthorizationStatusAuthorizedAlways: 
  7.       NSLog(@"Always Authorized"); 
  8.       break; 
  9.   case kCLAuthorizationStatusAuthorizedWhenInUse: 
  10.       NSLog(@"AuthorizedWhenInUse"); 
  11.       break; 
  12.   case kCLAuthorizationStatusDenied: 
  13.       NSLog(@"Denied"); 
  14.       break; 
  15.   case kCLAuthorizationStatusNotDetermined: 
  16.       NSLog(@"not Determined"); 
  17.       break; 
  18.   case kCLAuthorizationStatusRestricted: 
  19.       NSLog(@"Restricted"); 
  20.       break; 
  21.   default
  22.       break; 

获取定位权限

  1. CLLocationManager *manager = [[CLLocationManager alloc] init]; 
  2. [manager requestAlwaysAuthorization];//一直获取定位信息 
  3. [manager requestWhenInUseAuthorization];//使用的时候获取定位信息 

在代理方法中查看权限是否改变

  1. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ 
  2.  switch (status) { 
  3.   case kCLAuthorizationStatusAuthorizedAlways: 
  4.       NSLog(@"Always Authorized"); 
  5.       break; 
  6.   case kCLAuthorizationStatusAuthorizedWhenInUse: 
  7.       NSLog(@"AuthorizedWhenInUse"); 
  8.       break; 
  9.   case kCLAuthorizationStatusDenied: 
  10.       NSLog(@"Denied"); 
  11.       break; 
  12.   case kCLAuthorizationStatusNotDetermined: 
  13.       NSLog(@"not Determined"); 
  14.       break; 
  15.   case kCLAuthorizationStatusRestricted: 
  16.       NSLog(@"Restricted"); 
  17.       break; 
  18.   default
  19.       break; 
  20.   } 
  21.  

推送权限

检查是否有通讯权限

  1. UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings]; 
  2. switch (settings.types) { 
  3.   case UIUserNotificationTypeNone: 
  4.       NSLog(@"None"); 
  5.       break; 
  6.   case UIUserNotificationTypeAlert: 
  7.       NSLog(@"Alert Notification"); 
  8.       break; 
  9.   case UIUserNotificationTypeBadge: 
  10.       NSLog(@"Badge Notification"); 
  11.       break; 
  12.   case UIUserNotificationTypeSound: 
  13.       NSLog(@"sound Notification'"); 
  14.       break; 
  15.  
  16.   default
  17.       break; 

获取推送权限

  1. UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil]; 
  2. [[UIApplication sharedApplication] registerUserNotificationSettings:setting]; 

通讯录权限

 iOS9.0之前

导入头文件 @import AddressBook;

检查是否有通讯录权限

  1. ABAuthorizationStatus ABstatus = ABAddressBookGetAuthorizationStatus(); 
  2. switch (ABstatus) { 
  3.   case kABAuthorizationStatusAuthorized: 
  4.       NSLog(@"Authorized"); 
  5.       break; 
  6.   case kABAuthorizationStatusDenied: 
  7.       NSLog(@"Denied'"); 
  8.       break; 
  9.   case kABAuthorizationStatusNotDetermined: 
  10.       NSLog(@"not Determined"); 
  11.       break; 
  12.   case kABAuthorizationStatusRestricted: 
  13.       NSLog(@"Restricted"); 
  14.       break; 
  15.   default
  16.       break; 

获取通讯录权限

  1. ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULLNULL); 
  2. ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { 
  3.   if (granted) { 
  4.       NSLog(@"Authorized"); 
  5.       CFRelease(addressBook); 
  6.   }else
  7.       NSLog(@"Denied or Restricted"); 
  8.   } 
  9. }); 

iOS9.0及以后

导入头文件 @import Contacts;

检查是否有通讯录权限

  1. CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]; 
  2.   switch (status) { 
  3.         case CNAuthorizationStatusAuthorized: 
  4.         { 
  5.             NSLog(@"Authorized:"); 
  6.         } 
  7.             break; 
  8.         case CNAuthorizationStatusDenied:{ 
  9.             NSLog(@"Denied"); 
  10.         } 
  11.             break; 
  12.         case CNAuthorizationStatusRestricted:{ 
  13.             NSLog(@"Restricted"); 
  14.         } 
  15.             break; 
  16.         case CNAuthorizationStatusNotDetermined:{ 
  17.              NSLog(@"NotDetermined"); 
  18.         } 
  19.             break; 
  20.  
  21.        } 

获取通讯录权限

  1. CNContactStore *contactStore = [[CNContactStore alloc] init]; 
  2.     [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { 
  3.         if (granted) { 
  4.  
  5.            NSLog(@"Authorized"); 
  6.  
  7.         }else
  8.  
  9.            NSLog(@"Denied or Restricted"); 
  10.         } 
  11.     }]; 

日历、备忘录权限

导入头文件

检查是否有日历或者备忘录权限

  1. typedef NS_ENUM(NSUInteger, EKEntityType) { 
  2.  EKEntityTypeEvent,//日历 
  3.  EKEntityTypeReminder //备忘 
  4. }; 
  1. EKAuthorizationStatus EKstatus = [EKEventStore  authorizationStatusForEntityType:EKEntityTypeEvent]; 
  2. switch (EKstatus) { 
  3.   case EKAuthorizationStatusAuthorized: 
  4.       NSLog(@"Authorized"); 
  5.       break; 
  6.   case EKAuthorizationStatusDenied: 
  7.       NSLog(@"Denied'"); 
  8.       break; 
  9.   case EKAuthorizationStatusNotDetermined: 
  10.       NSLog(@"not Determined"); 
  11.       break; 
  12.   case EKAuthorizationStatusRestricted: 
  13.       NSLog(@"Restricted"); 
  14.       break; 
  15.   default
  16.       break; 

获取日历或备忘录权限

  1. EKEventStore *store = [[EKEventStore alloc]init]; 
  2. [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) { 
  3.   if (granted) { 
  4.       NSLog(@"Authorized"); 
  5.   }else
  6.       NSLog(@"Denied or Restricted"); 
  7.   } 
  8. }]; 

 最后一点

素有获取权限的方法,多用于用户第一次操作应用,iOS 8.0之后,将这些设置都整合在一起,并且可以开启或关闭相应的权限。所有的权限都可以通过下面的方法打开:

  1. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; 

上述的权限多为经常用到的权限,当然不会很全面,大家如有需要其他的权限,可以在下方评论,我会及时加上去的。

希望这篇文章能够给大家的开发带来一些便利。

责任编辑:未丽燕 来源: 简书
相关推荐

2019-08-28 08:57:05

2021-10-12 10:50:31

鸿蒙HarmonyOS应用

2020-04-14 08:46:47

Java对象编译器

2018-08-10 05:06:03

提速降费营运商漫游

2023-06-16 14:10:00

TCPUDP网络通信

2021-01-29 17:07:26

排序算法数组

2021-10-10 20:36:49

Android Root权限

2019-11-20 15:40:48

CPU软件处理器

2022-05-06 09:21:21

TypeScriptinterfacetype

2011-06-14 12:56:55

SQL Server复灾

2022-04-07 08:20:22

typeinterface前端

2022-06-07 08:14:35

PGPAGETUPLE

2022-11-28 07:10:57

2022-06-06 07:58:52

勒索软件恶意软件解密

2022-01-06 07:59:32

WebGPUOpenGL引擎

2021-03-05 18:38:45

ESvue项目

2022-03-08 15:01:48

负载均衡IP服务器

2020-06-23 10:28:30

软件研发交付

2023-10-27 07:39:44

IOC容器Spring

2022-08-15 07:24:41

WindowsDLL键盘
点赞
收藏

51CTO技术栈公众号