iOS隐私权限设置(相机,相册,麦克风,定位,通讯录)
2020-05-01 本文已影响0人
Peter杰
最近项目中用到系统隐私权限的管理,所以对iOS常用隐私权限做一下总结,方便以后查阅
iOS常用隐私权限
<key>NSBluetoothPeripheralUsageDescription</key>
<string>需要获取蓝牙权限</string>
<key>NSCalendarsUsageDescription</key>
<string>日历</string>
<key>NSCameraUsageDescription</key>
<string>需要获取您的摄像头信息</string>
<key>NSContactsUsageDescription</key>
<string>需要获取您的通讯录权限</string>
<key>NSHealthShareUsageDescription</key>
<string>健康分享权限</string>
<key>NSHealthUpdateUsageDescription</key>
<string>健康数据更新权限</string>
<key>NSHomeKitUsageDescription</key>
<string>HomeKit权限</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>一直定位权限</string>
<key>NSLocationUsageDescription</key>
<string>定位权限</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>使用app期间定位权限</string>
<key>NSMicrophoneUsageDescription</key>
<string>需要获取您的麦克风权限</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>需要获取您的相册信息</string>
<key>NSRemindersUsageDescription</key>
<string>提醒事项</string>
<key>NSSiriUsageDescription</key>
<string>需要获取您的Siri权限</string>
<key>NSSpeechRecognitionUsageDescription</key>
<string>语音识别权限</string>
<key>NSVideoSubscriberAccountUsageDescription</key>
<string>AppleTV权限</string>
<key>NSAppleMusicUsageDescription</key>
<string>Add tracks to your music library.</string>
<key>NSMotionUsageDescription</key>
<string>运动与健身权限</string>
跳转到设置界面
- (void)openSettings{
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL: url options:@{} completionHandler:^(BOOL success) {
dispatch_async(dispatch_get_main_queue(), ^{
[self loadData];
});
}];
}
}
相机权限
- 相机权限判断是否开启
//Privacy - Camera
+ (BOOL)cameraPrivacy{
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied||status ==AVAuthorizationStatusNotDetermined)
{
// 无权限
// do something...
return NO;
}
return YES;
}
- 相机权限授权
- (void)requestCameraPrivacy{
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (status == AVAuthorizationStatusNotDetermined){
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[self loadData];
});
}
}];
}else{
[self openSettings];
}
}
相册权限
- 相册权限判断是否开启
+ (BOOL)photoLibraryPrivacy{
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied||status ==PHAuthorizationStatusNotDetermined)
{
// 无权限
// do something...
return NO;
}
return YES;
}
- 相册权限授权
- (void)requestPhotoLibraryPrivacy{
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status ==PHAuthorizationStatusNotDetermined)
{
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
dispatch_async(dispatch_get_main_queue(), ^{
[self loadData];
});
}];
}else{
[self openSettings];
}
}
麦克风权限
- 麦克风权限判断是否开启
+ (BOOL)microPhoneAuthPrivacy{
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied||status ==AVAuthorizationStatusNotDetermined)
{
// 无权限
// do something...
return NO;
}
return YES;
}
- 授权
//麦克风
-(void) requestMicroPhoneAuth
{
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
if (status == AVAuthorizationStatusNotDetermined){
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[self loadData];
});
}
}];
}else{
[self openSettings];
}
}
定位权限
- 定位权限判断是否开启
+ (BOOL)locationPrivacy{
if ([CLLocationManager locationServicesEnabled]) {
CLAuthorizationStatus status = CLLocationManager.authorizationStatus;
if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse||
status == kCLAuthorizationStatusAuthorizedAlways) {
return YES;
}
return NO;
}
return NO;
}
- 授权
注意: CLLocationManager 需要写成全局对象,否次会授权框会消失
- (void)requestLocationPrivacy{
self.locationManager = [CLLocationManager new];
if ([CLLocationManager locationServicesEnabled]) {
CLAuthorizationStatus status = CLLocationManager.authorizationStatus;
if (status == kCLAuthorizationStatusNotDetermined) {
[self.locationManager requestWhenInUseAuthorization];
}else{
[self openSettings];
}
}
}
通讯录权限
- 通讯录权限判断是否开启
+(BOOL)contactsPrivacy{
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
return status == CNAuthorizationStatusAuthorized;
}
- 授权
- (void)requestAuthorizationForAddressBook {
CNAuthorizationStatus authorizationStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if(authorizationStatus == CNAuthorizationStatusNotDetermined) {
CNContactStore *contactStore = [[CNContactStore alloc] init];
[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if(granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[self loadData];
});
} else{
NSLog(@"授权失败, error=%@", error);
}
}];
}else{
[self openSettings];
}
}