macOS 开发 - NSMicrophoneUsageDescription (10.14 权限问题)

版权声明:本文为博主原创文章,转载请附上本文链接地址。from : https://blog.csdn.net/lovechris00 https://blog.csdn.net/lovechris00/article/details/83542730

文章目录


升级 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]];
}

猜你喜欢

转载自blog.csdn.net/lovechris00/article/details/83542730