iOS隐私权限类别如下:
麦克风权限:Privacy - Microphone Usage Description 是否允许此App使用你的麦克风?
相机权限: Privacy - Camera Usage Description 是否允许此App使用你的相机?
相册权限: Privacy - Photo Library Usage Description 是否允许此App访问你的媒体资料库?
通讯录权限: Privacy - Contacts Usage Description 是否允许此App访问你的通讯录?
蓝牙权限:Privacy - Bluetooth Peripheral Usage Description 是否许允此App使用蓝牙?
语音转文字权限:Privacy - Speech Recognition Usage Description 是否允许此App使用语音识别?
日历权限:Privacy - Calendars Usage Description
定位权限:Privacy - Location When In Use Usage Description
定位权限: Privacy - Location Always Usage Description
位置权限:Privacy - Location Usage Description
媒体库权限:Privacy - Media Library Usage Description
健康分享权限:Privacy - Health Share Usage Description
健康更新权限:Privacy - Health Update Usage Description
运动使用权限:Privacy - Motion Usage Description
音乐权限:Privacy - Music Usage Description
提醒使用权限:Privacy - Reminders Usage Description
Siri使用权限:Privacy - Siri Usage Description
电视供应商使用权限:Privacy - TV Provider Usage Description
视频用户账号使用权限:Privacy - Video Subscriber Account Usage Description
网络权限:App Transport Security Settings — Allow Arbitrary Loads
常用的几种权限设置
一、相机权限:
判断是否已开启相机权限yes是开启:
- (BOOL)requestCaptureDeviceAuthor {AVAuthorizationStatus authStatus =[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];if (authStatus == AVAuthorizationStatusDenied ||authStatus == AVAuthorizationStatusRestricted ||authStatus == AVAuthorizationStatusNotDetermined) {return NO;}return YES;
}
判断是否已询问相机权限:
authStatus==AVAuthorizationStatusNotDetermined时表示为询问相机权限,需要系统弹框提示开启或拒绝:调用requestAccessForMediaType方法就会掉漆系统弹框询问,如下代码:
AVAuthorizationStatus authStatus =[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusNotDetermined) {// 回调函数在任意队列,包括分线程。[AVCaptureDevicerequestAccessForMediaType:AVMediaTypeVideocompletionHandler:^(BOOL granted) {dispatch_async(dispatch_get_main_queue(), ^{if (granted) {// 再次判断用户是否同意授权authorityPermisstion =[self requestCaptureDeviceAuthor];进行跳转或其他操作。}});}];return;
}
二、相册权限:
判断是否已开启相册权限yes是开启:
#import <Photos/Photos.h>
- (BOOL)photoAuthority {PHAuthorizationStatus photoAuthStatus = [PHPhotoLibrary authorizationStatus];if (photoAuthStatus ==PHAuthorizationStatusNotDetermined) { // 未询问是否授权 可以用下面的请求授权方法询问用户return NO;} else if (photoAuthStatus == PHAuthorizationStatusRestricted ||photoAuthStatus == PHAuthorizationStatusDenied) { // 未授权return NO;}// 已授权return YES;
}
当PHAuthorizationStatus == PHAuthorizationStatusNotDetermined代表权限未询问,需要调用PHPhotoLibrary requestAuthorization方法进行弹框询问:
PHAuthorizationStatus oldStatus = [PHPhotoLibrary authorizationStatus];
if (oldStatus == PHAuthorizationStatusNotDetermined) {[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {dispatch_async(dispatch_get_main_queue(), ^{if (status != PHAuthorizationStatusNotDetermined) {// 再次判断相册当前权限状态是否已开启authorityPermisstion = [self photoAuthority];进行其他操作}});}];
}
三、通讯录权限
判断是否已开启通讯录权限yes是开启:
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
- (BOOL)addressBookAuthority {if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] != CNAuthorizationStatusAuthorized) {return NO;}return YES;
}
当[CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] ==CNAuthorizationStatusNotDetermined时表示权限未询问,通过调用requestAccessForEntityType来进行弹框询问,如下代码:
if ([CNContactStoreauthorizationStatusForEntityType:CNEntityTypeContacts] ==CNAuthorizationStatusNotDetermined) {// 未询问CNContactStore* contactStore = [[CNContactStore alloc] init];[contactStorerequestAccessForEntityType:CNEntityTypeContactscompletionHandler:^(BOOL granted, NSError* _Nullable error) {[ISTools performCodeAtMainTheard:^{if (granted) {// 再次判断是否已开启通讯录权限authorityPermisstion = [self addressBookAuthority];进行其他操作}}];}];}
四、麦克风权限
判断麦克风权限是否已开启
#import <AVFoundation/AVFoundation.h>
- (BOOL)audioAuthority {BOOL state = YES;AVAuthorizationStatus authStatus =[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];switch (authStatus) {case AVAuthorizationStatusNotDetermined: { // 没有询问是否开启麦克风state = NO;} break;case AVAuthorizationStatusRestricted: { // 未授权,家长限制state = NO;} break;case AVAuthorizationStatusDenied: { // 玩家未授权 提示:“亲,麦克风权限已关闭,请在系统“设置”-“隐私”-“麦克风”-“热门直播”里开启”state = NO;} break;case AVAuthorizationStatusAuthorized: { // 玩家授权state = YES;} break;}return state;
}
当authStatus == AVAuthorizationStatusNotDetermined时,代表权限从未询问过,需调用requestAccessForMediaType方法进行弹框询问,如下代码:
AVAuthorizationStatus authStatus =[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
if (authStatus == AVAuthorizationStatusNotDetermined) {[AVCaptureDevicerequestAccessForMediaType:AVMediaTypeAudiocompletionHandler:^(BOOL granted) {dispatch_async(dispatch_get_main_queue(), ^{if (granted) {// 用户同意授权authorityPermisstion = [self audioAuthority];进行其他操作}});}];
}
五、地理位置权限
判断是否已开启地理位置使用权限,yes已允许使用位置:
#import <CoreLocation/CoreLocation.h>
- (BOOL)isOpenLocation {if ([CLLocationManager locationServicesEnabled] &&([CLLocationManager authorizationStatus] ==kCLAuthorizationStatusAuthorizedAlways ||[CLLocationManager authorizationStatus] ==kCLAuthorizationStatusAuthorizedWhenInUse)) {return YES;} else {return NO;}
}
当[CLLocationManager locationServicesEnabled] &&CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined时代表从未询问过位置权限,需requestWhenInUseAuthorization方法进行弹框询问,如下代码:
if ([CLLocationManager locationServicesEnabled] &&[CLLocationManager authorizationStatus] ==kCLAuthorizationStatusNotDetermined) {if (!self.locManager) {self.locManager = [[CLLocationManager alloc] init];self.locManager.delegate = self;}[self.locManager requestWhenInUseAuthorization];return;
}
弹框询问是否开启使用位置后,通过CLLocationManagerdelegate方法-- (void)locationManager:(CLLocationManager*)manager
didChangeAuthorizationStatus:(CLAuthorizationStatus)status进行回调得知询问的结果status。
#pragma mark--CLLocationManagerdelegate
- (void)locationManager:(CLLocationManager*)managerdidChangeAuthorizationStatus:(CLAuthorizationStatus)status {if (status == kCLAuthorizationStatusAuthorizedAlways ||status == kCLAuthorizationStatusAuthorizedWhenInUse) {
// [self pushAuthoritySetViewControllerWithTitle:@"地理位置"
// authorityDescString:
// @"若您授权地理位置权限,该权限将用于发现附近"
// @"的直播内容,当您开播时也会推荐给附近的人观"
// @"看,建议您允许以保证体验的流畅"
// authorityPermisstion:[self isOpenLocation]]; }
}