一、权限汇总
隐私权限 |
版本 |
Infor.plist字段 |
定位服务 |
* |
Privacy - Location Always Usage Description 、Privacy - Location When In Use Usage Description 、Privacy - 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 Description 、Privacy - Health Update Usage Description |
Home kit |
iOS8 ~ |
Privacy - HomeKit Usage Description |
媒体与Apple Music |
iOS 9 ~ |
Privacy - Media Library Usage Description |
三、权限设置相关代码汇总
- 定位服务
BOOL enable = [CLLocationManager locationServicesEnabled];//是否可用
NSInteger state = [CLLocationManager authorizationStatus];//授权状态
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager requestAlwaysAuthorization];
[self.locationManager requestWhenInUseAuthorization];
- 通讯录
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
CNContactStore *contactStore = [[CNContactStore alloc] init];
[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {}];
- 日历
EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {}];
- 提醒事项
EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError * _Nullable error) {
}];
- 照片
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) {
}];
}
- 蓝牙
self.cbCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
#pragma mark - CBCentralManagerDelegate
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
CBManagerState state = central.state;
}
}
- 麦克风
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {}];
- 相机
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
}
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
}];
- 运动与健身
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) {}];
- 语音识别
SFSpeechRecognizerAuthorizationStatus status = [SFSpeechRecognizer authorizationStatus];
[SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
}];
- 健康
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) {}];
- 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) {}];
- 媒体与Apple Music
SKCloudServiceAuthorizationStatus status = [SKCloudServiceController authorizationStatus];
[SKCloudServiceController requestAuthorization:^(SKCloudServiceAuthorizationStatus status) {}];
- 面容、指纹
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(@"认证成功!");
}
}];