Mac开发-摄像头麦克风权限获取

使用AVCaptureDevice authorizationStatusForMediaType判断摄像头和麦克风的授权状态。
使用AVCaptureDevice requestAccessForMediaType:获取摄像头和麦克风的授权

权限获取

系统方法都很清楚,这里列出工具代码供参考:

#import 

NS_ASSUME_NONNULL_BEGIN

@interface PrivacyAuthorizer : NSObject

+ (void)authorCamera:(void (^ __nullable)(BOOL granted))completion;

+ (void)authorMicphone:(void (^ __nullable)(BOOL granted))completion;

@end

NS_ASSUME_NONNULL_END
//
//  PrivacyAuthorizer.m
//  Webcast
//
//  Created by net263 on 2019/12/17.
//  Copyright © 2019 Gensee Inc. All rights reserved.
//

#import "PrivacyAuthorizer.h"
#import 
@implementation PrivacyAuthorizer

+ (void)authorCamera:(void (^ __nullable)(BOOL granted))completion{
     
    dispatch_block_t workBlock;
    if (@available(macOS 10.14, *)) {
     
        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if(authStatus == AVAuthorizationStatusAuthorized) {
     
            workBlock = ^{
     
                if (completion) completion(YES);
            };
            // do your logic
        } else if(authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted){
     
            workBlock = ^{
     
                if (completion) completion(NO);
            };
            // denied
        } else if(authStatus == AVAuthorizationStatusNotDetermined){
     
            // not determined?!
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
     
                [PrivacyAuthorizer authorCamera:completion];
            }];
            return;
        } else {
     
            // impossible, unknown authorization status
        }
    }else {
     
        workBlock = ^{
     
            if (completion) completion(YES);
        };
    }
    dispatch_async(dispatch_get_main_queue(), workBlock);
}

+ (void)authorMicphone:(void (^ __nullable)(BOOL granted))completion{
     
    dispatch_block_t workBlock;
    if (@available(macOS 10.14, *)) {
     
        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
        if(authStatus == AVAuthorizationStatusAuthorized) {
     
            workBlock = ^{
     
                if (completion) completion(YES);
            };
            // do your logic
        } else if(authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted){
     
            workBlock = ^{
     
                if (completion) completion(NO);
            };
            // denied
        } else if(authStatus == AVAuthorizationStatusNotDetermined){
     
            // not determined?!
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
     
                [PrivacyAuthorizer authorMicphone:completion];
            }];
            return;
        } else {
     
            // impossible, unknown authorization status
        }
    }else {
     
        workBlock = ^{
     
            if (completion) completion(YES);
        };
    }
    dispatch_async(dispatch_get_main_queue(), workBlock);
}

@end

plist文件设置

获取权限同时需要在plist文件中添加字段
Privacy - Camera Usage Description或者NSCameraUsageDescription
Privacy - Microphone Usage Description 或者 NSMicrophoneUsageDescription

在这里插入图片描述

你可能感兴趣的:(Mac开发,mac)