zoukankan      html  css  js  c++  java
  • iOS开发中的权限

    权限分类

    • 联网权限

    • 相册权限

    • 相机、麦克风权限

    • 定位权限

    • 推送权限

    • 通讯录权限

    • 日历、备忘录权限

    联网权限

    引入头文件 @import CoreTelephony;

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    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
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    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;
    }

    相册权限

    iOS 9.0之前

    导入头文件@import AssetsLibrary;

    检查是否有相册权限

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    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;

    检查是否有相册权限

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    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;
    }

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

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

    相机和麦克风权限

    导入头文件@import AVFoundation;

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    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;
    }

    获取相机或麦克风权限

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [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中进行配置;

    配置文件

    检查是否有定位权限

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
     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;
    }

    获取定位权限

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

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    - (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;
      }
       
    }

    推送权限

    检查是否有通讯权限

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    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;
    }

    获取推送权限

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

    通讯录权限

    iOS 9.0之前

    导入头文件 @import AddressBook;

    检查是否有通讯录权限

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    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;
    }

    获取通讯录权限

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

    iOS 9.0及以后

    导入头文件 @import Contacts;

    检查是否有通讯录权限

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    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;
                 
           }

    获取通讯录权限

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

    日历、备忘录权限

    导入头文件

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

    1
    2
    3
    4
     typedef NS_ENUM(NSUInteger, EKEntityType) {
      EKEntityTypeEvent,//日历
      EKEntityTypeReminder //备忘
     };
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    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;
    }

    获取日历或备忘录权限

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

    最后一点

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

    1
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString
  • 相关阅读:
    数据结构与算法JavaScript (一) 栈
    js架构设计模式——前端MVVM框架设计及实现(二)
    js架构设计模式——前端MVVM框架设计及实现(一)
    js架构设计模式——MVC,MVP 和 MVVM 的图示及简单明了的区别说明
    js架构设计模式——你对MVC、MVP、MVVM 三种组合模式分别有什么样的理解?
    js面向对象oop编程
    js模块化开发——前端模块化
    SPRING 集成 activemq 的 topic 模式
    linux yum 本地源配置
    ORACLE 导入的问题
  • 原文地址:https://www.cnblogs.com/weiboyuan/p/6023089.html
Copyright © 2011-2022 走看看