Mac开发-mac sdk10.13以下版本的麦克风和摄像头权限获取

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/shengpeng3344/article/details/102728298

官方文档大于一切
https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/requesting_authorization_for_media_capture_on_macos?language=objc

使用AuthorizationauthorizationStatusForMediaType即可获取权限

/*!
 @method authorizationStatusForMediaType:
 @abstract
    Returns the client's authorization status for accessing the underlying hardware that supports a given media type.
 
 @param mediaType
    The media type, either AVMediaTypeVideo or AVMediaTypeAudio
 @result
    The authorization status of the client
 
 @discussion
    This method returns the AVAuthorizationStatus of the client for accessing the underlying hardware supporting the media type. Media type constants are defined in AVMediaFormat.h. If any media type other than AVMediaTypeVideo or AVMediaTypeAudio is supplied, an NSInvalidArgumentException will be thrown. If the status is AVAuthorizationStatusNotDetermined, you may use the +requestAccessForMediaType:completionHandler: method to request access by prompting the user.
 */
+ (AVAuthorizationStatus)authorizationStatusForMediaType:(AVMediaType)mediaType API_AVAILABLE(macos(10.14), ios(7.0));

处理代码示例:

AVAuthorizationStatus authorState = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
if (authorState == AVAuthorizationStatusRestricted ||authorState == AVAuthorizationStatusDenied) {
    NSAlert* alert = [[NSAlert alloc]init];
    [alert setMessageText:NSLocalizedString(@"author.micphone", nil)];
    [alert addButtonWithTitle:NSLocalizedString(@"author.btn.setting", nil)];
    [alert addButtonWithTitle:NSLocalizedString(@"click.alert.reject", nil)];
    [alert beginSheetModalForWindow:[self window] completionHandler:^(NSModalResponse returnCode) {
        if (returnCode == NSAlertFirstButtonReturn) {
            NSString *urlString = @"x-apple.systempreferences:com.apple.preference.security?Privacy_Microphone";
            [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:urlString]];
        }
    }];
}else {
     //normal logic
}

但这个方法只在macos10.14才会出现,那么我们自动化编译的机型一般都不会实时更新,版本低于10.14,更新还需要更新系统,和一些其他问题。那么如何能让低版本的编译通过并兼容。

即使用运行时解决:

if (@available(macOS 10.14, *)) { 
    //before 10.14 ,authorizationStatusForMediaType is not exist, so use runtime to avoid build error
    //if xcode version is lower than 9.0,use NSAppKitVersionNumber > 1671,1671 is show in xcode 11
    Class cls = objc_getClass("AVCaptureDevice");
    if (!cls) {
        //normal logic
        return;
    }
    NSInteger (*authorStateGet)(id, SEL,id) = (NSInteger (*)(id, SEL,id))objc_msgSend;
    NSInteger authorState = authorStateGet(cls,sel_registerName("authorizationStatusForMediaType:"),@"soun");
    //            AVAuthorizationStatus authorState = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    if (authorState == 1 ||authorState == 2) {
        NSAlert* alert = [[NSAlert alloc]init];
        [alert setMessageText:NSLocalizedString(@"author.micphone", nil)];
        [alert addButtonWithTitle:NSLocalizedString(@"author.btn.setting", nil)];
        [alert addButtonWithTitle:NSLocalizedString(@"click.alert.reject", nil)];
        [alert beginSheetModalForWindow:[self window] completionHandler:^(NSModalResponse returnCode) {
            if (returnCode == NSAlertFirstButtonReturn) {
                NSString *urlString = @"x-apple.systempreferences:com.apple.preference.security?Privacy_Microphone";
                [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:urlString]];
            }
        }];
    }else {
        //normal logic
    }
} else {
    // Fallback on earlier versions
    //normal logic
}

这里在一台支持10.14的机型上打印

NSLog(@"%@ %@",AVMediaTypeAudio,AVMediaTypeVideo);

得到的值为sounvide,但愿这个值是不变的,万一版本变动改变了就坑爹了。

另外@available(macOS 10.14, *)是xcode9.0之后才有的,如果使用8.0的版本xcode,可以使用NSAppKitVersionNumber > 1671,这里的1671值是参考高版本中sdk的枚举定义,也是正确的,表示为10.14版本

这样在mac os sdk 10.13版本顺利通过编译

猜你喜欢

转载自blog.csdn.net/shengpeng3344/article/details/102728298