本帖最后由 代码买卖 于 2016-7-13 20:03 编辑



00、写在前面

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

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

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


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

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

iOS开发中的这九大权限的检查、获取,你搞懂了吗? 源代码 - 敏捷大拇指 - iOS开发中的这些权限,你搞懂了吗? 0





0、权限分类

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





1、联网权限


1.1、引入头文件

[Objective-C] 纯文本查看 复制代码
@import CoreTelephony;



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

[Objective-C] 纯文本查看 复制代码
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;
  };
};



1.3、查询应用是否有联网功能

[Objective-C] 纯文本查看 复制代码
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;
}





2、相册权限



2.1、相册权限--iOS 9.0之前


2.1.1、导入头文件

[Objective-C] 纯文本查看 复制代码
@import AssetsLibrary;



2.1.2、检查是否有相册权限

[Objective-C] 纯文本查看 复制代码
 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;
}




2.2、相册权限--iOS 8.0之后


2.2.1、导入头文件

[Objective-C] 纯文本查看 复制代码
@import Photos;



2.2.2、检查是否有相册权限

[Objective-C] 纯文本查看 复制代码
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;
}



2.2.3、获取相册权限

[Objective-C] 纯文本查看 复制代码
![Uploading 144446-b8aca7ba38c5f8c0_695906.png . . .]

 [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  if (status == PHAuthorizationStatusAuthorized) {
      NSLog(@"Authorized");
  }else{
      NSLog(@"Denied or Restricted");
  }
  }];





3、相机和麦克风权限


3.1、导入头文件

[Objective-C] 纯文本查看 复制代码
@import AVFoundation;



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

[Objective-C] 纯文本查看 复制代码
AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];//相机权限
AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//麦克风权限

switch (AVstatus) {
  case AVAuthorizationStatusAuthorized:
      NSLog(@"Authorized");
      break;
  case AVAuthorizationStatusDenied:
      NSLog(@"Denied");
      break;
  case AVAuthorizationStatusNotDetermined:
      NSLog(@"not Determined");
      break;
  case AVAuthorizationStatusRestricted:
      NSLog(@"Restricted");
      break;
  default:
      break;
}



3.3、获取相机或麦克风权限

[Objective-C] 纯文本查看 复制代码
[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、定位权限


4.1、导入头文件

[Objective-C] 纯文本查看 复制代码
@import CoreLocation;



4.2、在info.plist中进行配置

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

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

iOS开发中的这九大权限的检查、获取,你搞懂了吗? 源代码 - 敏捷大拇指 - iOS开发中的这些权限,你搞懂了吗? 1

配置文件


4.3、检查是否有定位权限

[Objective-C] 纯文本查看 复制代码
 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;
}



4.4、获取定位权限

[Objective-C] 纯文本查看 复制代码
CLLocationManager *manager = [[CLLocationManager alloc] init];
[manager requestAlwaysAuthorization];//一直获取定位信息
[manager requestWhenInUseAuthorization];//使用的时候获取定位信息



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

[Objective-C] 纯文本查看 复制代码
- (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;
  }

}





5、推送权限


5.1、检查是否有通讯权限

[Objective-C] 纯文本查看 复制代码
  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;
}



5.2、获取推送权限

[Objective-C] 纯文本查看 复制代码
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];





6、通讯录权限


6.1、导入头文件

[Objective-C] 纯文本查看 复制代码
@import AddressBook;



6.2、检查是否有通讯录权限

[Objective-C] 纯文本查看 复制代码
 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;
}



6.3、获取通讯录权限

[Objective-C] 纯文本查看 复制代码
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
  if (granted) {
      NSLog(@"Authorized");
      CFRelease(addressBook);
  }else{
      NSLog(@"Denied or Restricted");
  }
});





7、日历、备忘录权限


7.1、导入头文件


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

[Objective-C] 纯文本查看 复制代码
 typedef NS_ENUM(NSUInteger, EKEntityType) {
  EKEntityTypeEvent,//日历
  EKEntityTypeReminder //备忘
 };


[Objective-C] 纯文本查看 复制代码
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;
}



7.3、获取日历或备忘录权限

[Objective-C] 纯文本查看 复制代码
EKEventStore *store = [[EKEventStore alloc]init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {
  if (granted) {
      NSLog(@"Authorized");
  }else{
      NSLog(@"Denied or Restricted");
  }
}];





8、最后一点

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

[Objective-C] 纯文本查看 复制代码
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];


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

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




相关内容

iOS开发中的这九大权限的检查、获取,你搞懂了吗? 源代码

史上最全的iOS中获取各种设备信息的方法总结 源代码下载




作者:Jack_lin