OC学习56:iOS权限设置

张建 lol

相册 权限

  • iOS11之前:访问相册(读权限)存储照片到相册(写权限),需要用户授权,需要添加Privacy - Photo Library Usage Description(读权限)和 Privacy - Photo Library Additions Usage Description (写权限)

    • Privacy - Photo Library Usage Description 读权限: App需要更换头像图片,是否允许访问您的相册?
    • Privacy - Photo Library Additions Usage Description 写权限: App需要添加图片到相册,是否允许访问您的相册?
  • iOS11之后:默认开启访问相册权限(读权限),无需用户授权,无需添加

注:适配iOS11之前的还是需要加的

  • 相册权限:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#pragma mark -检查相册的权限(相册流程与相机流程相同,相册是不存在硬件问题的,只要有权限就可以直接调用)
- (void)checkPhotoPermission{
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
// 如果未询问会走 PHAuthorizationStatusNotDetermined
if (status == PHAuthorizationStatusNotDetermined) {
// 调用 requestAuthorization 会弹窗询问是否授权
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
dispatch_async(dispatch_get_main_queue(), ^{
if (status == PHAuthorizationStatusAuthorized) {
// 有权限进入调起相册方法
[self selectPhoto];
}else {
[SVPManager showFailureAndStatus:@"没有权限打开相册"];
}
});
}];
} else if (status == PHAuthorizationStatusDenied ||
status == PHAuthorizationStatusRestricted) { // 否决/限制
dispatch_async(dispatch_get_main_queue(), ^{
[SVPManager showFailureAndStatus:JITLocalizedString(@"NoPhotoPermissions")];
});
} else {
// 有权限进入调起相册方法
[self selectPhoto];
}
}

相机 权限

  • iOS11之前:访问相机,需要用户授权,需要添加 Privacy - Camera Usage Description

    • Privacy - Camera Usage Description : App需要扫描二维码或拍照,是否允许访问您的相机?
  • iOS11之后:默认开启访问相机权限,无需用户授权,无需添加

注:适配iOS11之前的还是需要加的

  • 权限判断:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#pragma mark -检查相机的权限
- (void)checkCameraPermission{
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
// 如果未询问会走 AVAuthorizationStatusNotDetermined
if (status == AVAuthorizationStatusNotDetermined) {
// 调用 requestAccessForMediaType 会弹窗并询问是否开启相机权限
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
// 有权限进入调起相机方法
[self takeCamera];
}else {
[SVPManager showFailureAndStatus:@"没有权限打开相机"];
}
}];
} else if (status == AVAuthorizationStatusDenied ||
status == AVAuthorizationStatusRestricted) { // 否决/限制
dispatch_async(dispatch_get_main_queue(), ^{
[JITAppTool ShowInfo:JITLocalizedString(@"NoCameraPermissions") andIsSuccess:NO];
});
} else {
// 有权限进入调起相机方法
[self takeCamera];
}
}

麦克风 权限

访问 麦克风 需要用户授权,否则App会crash:

Privacy - Microphone Usage Description : App需要您的同意,才能访问麦克风,以便您干什么

判断麦克风是否授权:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#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 方法进行弹框询问

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
AVAuthorizationStatus authStatus =
[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
if (authStatus == AVAuthorizationStatusNotDetermined) {
[AVCaptureDevice
requestAccessForMediaType:AVMediaTypeAudio
completionHandler:^(BOOL granted) {
dispatch_async(dispatch_get_main_queue(), ^{
if (granted) {
Bool isOpenAudioAuth = [self audioAuthority];
NSLog(@"用户同意授权");
}else {
NSLog(@"用户没有同意授权");
}
});
}];
}

通讯录 权限

Privacy - Contacts Usage Description : App需要您的同意,才能访问通讯录,以便您干什么?

判断是否已开启通讯录权限

1
2
3
4
5
6
7
8
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
- (BOOL)addressBookAuthority {
if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] != CNAuthorizationStatusAuthorized) {
return NO;
}
return YES;
}

判断是否询问开启通讯录权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if ([CNContactStore
authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusNotDetermined) {
// 未询问
CNContactStore* contactStore = [[CNContactStore alloc] init];
[contactStore
requestAccessForEntityType:CNEntityTypeContacts
completionHandler:^(BOOL granted, NSError* _Nullable error) {
[ISTools performCodeAtMainTheard:^{
if (granted) {
// 再次判断是否已开启通讯录权限
Bool authorityPermisstion = [self addressBookAuthority];
// 进行其他操作
NSLog(@"用户同意授权");
}else {
NSLog(@"用户没有同意授权");
}
}];
}];
}

蓝牙 权限

Privacy - Bluetooth Peripheral Usage Description : App需要您的同意,才能访问蓝牙,以便您干什么?

语音转文字 权限

Privacy - Speech Recognition Usage Description : App需要您的同意,才能访问语音转文字,以便您干什么?

日历 权限

Privacy - Calendars Usage Description : App需要您的同意,才能访问日历,以便您干什么?

定位 权限

  • Privacy - Location When In Use Usage Description : App需要您的同意,才能在使用期间访问位置,以便您干什么

  • Privacy - Location Always Usage Description : 需要您的同意,才能始终访问位置,以便您干什么?

判断是否已开启地理位置权限:

1
2
3
4
5
6
7
8
9
10
11
12
13
#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方法进行弹框询问

1
2
3
4
5
6
7
8
9
10
11
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#pragma mark--CLLocationManagerdelegate
- (void)locationManager:(CLLocationManager*)manager
didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorizedAlways ||
status == kCLAuthorizationStatusAuthorizedWhenInUse) {
// [self pushAuthoritySetViewControllerWithTitle:@"地理位置"
// authorityDescString:
// @"若您授权地理位置权限,该权限将用于发现附近"
// @"的直播内容,当您开播时也会推荐给附近的人观"
// @"看,建议您允许以保证体验的流畅"
// authorityPermisstion:[self isOpenLocation]];
}
}

Siri 权限

Privacy - Siri Usage Description :App需要您的同意,才能访问Siri

提醒事项 权限

Privacy - Reminders Usage Description : App需要您的同意,才能访问提醒事项

健康 权限

  • Privacy - Motion Usage Description : 需要您的同意,才能访问运动与健身

  • Privacy - Health Update Usage Description : 需要您的同意,才能访问健康更新

  • Privacy - Health Share Usage Description : 需要您的同意,才能访问健康分享

媒体库 权限

Privacy - Media Library Usage Description : 需要您的同意,才能访问媒体库

音乐 权限

Privacy - Music Usage Description :App需要您的同意,才能访问音乐

电视供应商使用 权限

Privacy - TV Provider Usage Description :App需要您的同意,才能访问电视供应商使用

视频用户账号使用 权限

Privacy - Video Subscriber Account Usage Description : App需要您的同意,才能访问视频用户账号使用

网络 权限

App Transport Security Settings — Allow Arbitrary Loads : App需要您的同意,才能访问网络

  • Post title:OC学习56:iOS权限设置
  • Post author:张建
  • Create time:2023-05-31 09:10:13
  • Post link:https://redefine.ohevan.com/2023/05/31/OC/OC学习56:相机和相册权限/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.