iOS 隐私授权权限汇总

一、权限汇总

隐私权限 版本 Infor.plist字段
定位服务 * Privacy - Location Always Usage DescriptionPrivacy - Location When In Use Usage DescriptionPrivacy - Location Always and When In Use Usage Description
通讯录 * Privacy - Contacts Usage Description
日历 * Privacy - Calendars Usage Description
提醒事项 * Privacy - Reminders Usage Description
相册(读权限) * Privacy - Photo Library Usage Description
相册(写权限) * Privacy - Photo Library Additions Usage Description
蓝牙 * Privacy - Bluetooth Peripheral Usage Description
麦克风 * Privacy - Microphone Usage Description
相机 * Privacy - Camera Usage Description
本地网络 iOS14~ Privacy - Local Network Usage Description
面容/指纹 * Privacy - Face ID Usage Description
运动与健身 * Privacy - Motion Usage Description
语音识别(Siri) iOS10~ Privacy - Siri Usage Description
健康 iOS8 ~ Privacy - Health Share Usage DescriptionPrivacy - Health Update Usage Description
Home kit iOS8 ~ Privacy - HomeKit Usage Description
媒体与Apple Music iOS 9 ~ Privacy - Media Library Usage Description

三、权限设置相关代码汇总

  1. 定位服务
  • 获取状态
 BOOL enable = [CLLocationManager locationServicesEnabled];//是否可用
 NSInteger state = [CLLocationManager authorizationStatus];//授权状态
  • 申请权限
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager requestAlwaysAuthorization];
[self.locationManager requestWhenInUseAuthorization];
  1. 通讯录
  • 获取状态
 CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
  • 申请权限
 CNContactStore *contactStore = [[CNContactStore alloc] init];
 [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {}];
  1. 日历
  • 获取状态
 EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
  • 申请权限
 EKEventStore *store = [[EKEventStore alloc] init];
 [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {}];
  1. 提醒事项
  • 获取状态
 EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];
  • 申请权限
 EKEventStore *store = [[EKEventStore alloc] init];
 [store requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError * _Nullable error) {
 }];
  1. 照片
  • 获取状态
 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {    
     if (_isiOS8_Or_Later_) {
         PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
     } else {
         // iOS7 - iOS8
         ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
     }
 } else {
     NSLog(@"相册不可用!");
 }
  • 申请权限
 if (_isiOS8_Or_Later_) {
     [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
     }];
 } else {
     ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
     [assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
     } failureBlock:^(NSError *error) {
}];
 }
  1. 蓝牙
  • 获取状态
 self.cbCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
 #pragma mark - CBCentralManagerDelegate
 - (void)centralManagerDidUpdateState:(CBCentralManager *)central{
     CBManagerState state = central.state;
    }
 }
  • 申请权限
  1. 麦克风
  • 获取状态
 AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
  • 申请权限
 [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {}];
  1. 相机
  • 获取状态
 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
     AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
 }
  • 申请权限
 [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
 }];
  1. 运动与健身
  • 获取状态
 if( [CMMotionActivityManager isActivityAvailable]){
     CMAuthorizationStatus status = [CMMotionActivityManager authorizationStatus];
 }
  • 申请权限
 self.cmManager = [[CMMotionActivityManager alloc] init];
 self.motionActivityQueue = [[NSOperationQueue alloc] init];
 [self.cmManager startActivityUpdatesToQueue:self.motionActivityQueue withHandler:^(CMMotionActivity *activity) {}];
  1. 语音识别
  • 获取状态
 SFSpeechRecognizerAuthorizationStatus status = [SFSpeechRecognizer authorizationStatus];
  • 申请权限
 [SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
 }];
  1. 健康
  • 获取状态
 if ([HKHealthStore isHealthDataAvailable]) {
     self.healthStore = [[HKHealthStore alloc] init];
     // 以心率 HKQuantityTypeIdentifierHeartRate 为例子
     HKQuantityType *heartRateType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
     HKAuthorizationStatus status = [self.healthStore authorizationStatusForType:heartRateType];
 }else{
     NSLog(@"unavailable");
     // Health data is not avaliable on all device.
 }
  • 申请权限
 [self.healthStore requestAuthorizationToShareTypes:typeSet readTypes:typeSet completion:^(BOOL success, NSError * _Nullable error) {}];
  1. Home Kit
  • 获取状态
 self.homeManager = [[HMHomeManager alloc] init];
 self.homeManager.delegate = self;

 - (void)homeManagerDidUpdateHomes:(HMHomeManager *)manager{
     if (manager.homes.count > 0) {}
 }
  • 申请权限
 [manager addHomeWithName:@"Test Home" completionHandler:^(HMHome * _Nullable home, NSError * _Nullable error) {}];
  1. 媒体与Apple Music
  • 获取状态
 SKCloudServiceAuthorizationStatus status = [SKCloudServiceController authorizationStatus];
  • 申请权限
 [SKCloudServiceController requestAuthorization:^(SKCloudServiceAuthorizationStatus status) {}];
  1. 面容、指纹
  • 权限申请
YZAuthID *authID = [[YZAuthID alloc] init];
[authID yz_showAuthIDWithDescribe:nil block:^(YZAuthIDState state, NSError *error) {  
    if (state == YZAuthIDStateNotSupport) { // 不支持TouchID/FaceID
        NSLog(@"对不起,当前设备不支持指纹/面容ID");
    } else if(state == YZAuthIDStateFail) { // 认证失败
        NSLog(@"指纹/面容ID不正确,认证失败");
    } else if(state == YZAuthIDStateTouchIDLockout) {   // 多次错误,已被锁定
        NSLog(@"多次错误,指纹/面容ID已被锁定,请到手机解锁界面输入密码");
    } else if (state == YZAuthIDStateSuccess) { // TouchID/FaceID验证成功
        NSLog(@"认证成功!");
    }  
}];

你可能感兴趣的:(iOS 隐私授权权限汇总)