【iOS】自定义相机(二)权限获取

在自定义相机的时候,肯定是需要获取相机权限、麦克风权限和相册写入权限。权限获取这个在iOS开发中十分常见了,具体操作为:

  1. 在 info.plist 中添加获取权限描述
  2. 在需做要权限的操作前,先判断当前是否有权限
  3. 若没有权限需要引导用户修改权限

常用 info.plist 权限描述

<!-- 🖼 Photo Library -->
<key>NSPhotoLibraryUsageDescription</key>
<string><#Your description goes here#></string>

<!-- 🖼 Add Photo -->
<key>NSPhotoLibraryAddUsageDescription</key>
<string><#Your description goes here#></string>

<!-- 📷 Camera -->
<key>NSCameraUsageDescription</key>
<string><#Your description goes here#></string>

<!-- 🎤 Microphone -->
<key>NSMicrophoneUsageDescription</key>
<string><#Your description goes here#></string>

<!-- 📍 Location -->
<key>NSLocationUsageDescription</key>
<string><#Your description goes here#></string>

<!-- 📍 Location When In Use -->
<key>NSLocationWhenInUseUsageDescription</key>
<string><#Your description goes here#></string>

<!-- 📍 Location Always -->
<key>NSLocationAlwaysUsageDescription</key>
<string><#Your description goes here#></string>

<!-- 📆 Calendars -->
<key>NSCalendarsUsageDescription</key>
<string><#Your description goes here#></string>

<!-- ⏰ Reminders -->
<key>NSRemindersUsageDescription</key>
<string><#Your description goes here#></string>

<!-- 🏊 Motion -->
<key>NSMotionUsageDescription</key>
<string><#Your description goes here#></string>

<!-- 💊 Health Update -->
<key>NSHealthUpdateUsageDescription</key>
<string><#Your description goes here#></string>

<!-- 💊 Health Share -->
<key>NSHealthShareUsageDescription</key>
<string><#Your description goes here#></string>

<!-- ᛒ🔵 Bluetooth Peripheral -->
<key>NSBluetoothPeripheralUsageDescription</key>
<string><#Your description goes here#></string>

<!-- 🎵 Media Library -->
<key>NSAppleMusicUsageDescription</key>
<string><#Your description goes here#></string>

<!-- 👤 Contacts -->
<key>NSContactsUsageDescription</key>
<string><#Your description goes here#></string>

PS:<#Your description goes here#>中的内容即询问权限弹框中的描述,一般为引导用户给予 APP 权限的话语

相机和麦克风权限获取方式

在上一篇文章中,我们知道相机和麦克风都属于媒体捕获设备,即AVCaptureDevice。因此获取权限的步骤如下:

  1. 导入头文件:#import <AVFoundation/AVCaptureDevice.h>
  2. 调用[AVCaptureDevice authorizationStatusForMediaType:<#(nonnull AVMediaType)#>]
    • 获取相机权限传入AVMediaTypeVideo
    • 获取麦克风权限传入AVMediaTypeAudio

权限状态

权限状态AVAuthorizationStatus一般有以下四种情况:

  • AVAuthorizationStatusNotDetermined:权限未指定
    • 该状态表示用户尚未决定允许与否
  • AVAuthorizationStatusRestrict:权限受限制
    • 比如定位权限分为永久和应用使用期间,那么应用使用期间就表示权限受限制
  • AVAuthorizationStatusDenied:没有权限
  • AVAuthorizationStatusAuthorized:有权限

询问权限方式

在我看来有两种权限询问方式,分别是自动询问方式手动询问方式。个人认为后者更加好用。

自动询问方式

在第一次使用需要权限功能的时候,系统会自动弹出权限询问弹窗。比如我们开始获取相机捕获的数据时候,系统发现当前权限状态为AVAuthorizationStatusNotDetermined时候,会立即弹框询问用户是否给予权限。

手动询问方式

当真正执行到需要权限的操作再由系统强制弹出权限询问窗口,在用户看来怎么都觉得比较突然,如果用户选择了不允许,接下来的界面的显示比较尴尬。如果是没有相机权限,预览显示就直接是黑屏,用户体验不好。

使用下面这个方法可以在权限状态为AVAuthorizationStatusNotDetermined的时候,弹出系统权限询问弹框:

[AVCaptureDevice requestAccessForMediaType:<#(nonnull AVMediaType)#> completionHandler:<#^(BOOL granted)handler#>];
  • AVMediaType:传入需要的权限
  • ^(BOOL granted)handler:在这里可以获取用户的权限选择
    • 该Block并不在主线程中执行
    • 如果有 UI 层操作,必须回到主线程

跳转权限设置

询问权限弹框只会在权限为AVAuthorizationStatusNotDetermined时候由系统弹出,作为开发者的我们无法强制用户给予APP权限。一般都需要友好地引导用户进入设置界面进行权限给予。

常用跳转指定权限设置方式:

NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
    [[UIApplication sharedApplication] openURL:url];
}

PS:当用户在设置中修改了权限之后,该APP是必须会重新启动的。

SCCamera中权限获取界面

SCCamera中,我使用的权限获取界面相对简单,如下:

权限获取界面

权限获取方式代码(SCPermissionsView)如下:

- (void)obtainPermission:(UIButton*)sender {
    AVMediaType type = AVMediaTypeVideo;
    if (sender == self.microphoneBtn)
        type = AVMediaTypeAudio;
    switch ([AVCaptureDevice authorizationStatusForMediaType:type]) {
        case AVAuthorizationStatusNotDetermined: {
            // 手动询问
            [AVCaptureDevice requestAccessForMediaType:type completionHandler:^(BOOL granted) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self btnChange:sender granted:granted];
                });
            }];
            break;
        }
        case AVAuthorizationStatusDenied:
            // 跳转设置界面
            [self openSetting];
            break;
        default:
            break;
    }
}

官方文档

Requesting Authorization for Media Capture on iOS

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。