欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

macOS 开发 - NSMicrophoneUsageDescription

程序员文章站 2022-06-30 14:19:55
...

文章目录


升级 Mac 到 10.14 后运行项目提示:

 This app has crashed because it attempted to access privacy-sensitive data without a usage description.  The app's Info.plist must contain an NSMicrophoneUsageDescription key with a string value explaining to the user how the app uses this data.

需要在 Info.plist 中添加 NSMicrophoneUsageDescription 这个键和对应的描述,和 iOS 申请摄像头权限很像了。
这是因为 macOS 10.14 对隐私控制的更严格了,详情可参考 wwdc : https://developer.apple.com/videos/play/wwdc2018/702/?time=1049


那么设置 info.plist 之外,我们还需要检测 麦克风权限是否调用。

- (BOOL)isValidToVisitMicroPhone{
    
    NSString *version = [SystemTools getSystemVersion];
    
    if (version.floatValue < 10.14) {
        return YES;
    }
    
    NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
//    NSLog(@"videoDevices : %@",videoDevices);
    
    __block BOOL bCanRecord = YES;
    
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    NSLog(@"status : %d",status);
    
    switch (status) {
        case AVAuthorizationStatusNotDetermined:{
            
            // 许可对话没有出现,发起授权许可
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
                if (granted) {
                    //第一次用户接受
                    NSLog(@"-- granted");
                 
                }else{
                    //用户拒绝
                    NSLog(@"-- not granted");
                }
            }];
            break;
        }
            
        case AVAuthorizationStatusAuthorized:{
            // 已经开启授权,可继续
            NSLog(@"-- Authorized");
            bCanRecord = YES;
            break;
        }
        case AVAuthorizationStatusDenied:{
            NSLog(@"-- Denied");
            bCanRecord = NO;
            break;
        }
        case AVAuthorizationStatusRestricted:{
            NSLog(@"-- Restricted");
            bCanRecord = NO;
            break;
        }
            
        default:
            break;
    }
 
    return bCanRecord;
}

如果没调用,可以跳转到 隐私面板

- (void)openSetting{
    NSString *urlString = @"x-apple.systempreferences:com.apple.preference.security?Privacy_Microphone";
    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:urlString]];
}

上一篇: backlight一直存在

下一篇: nvm安装