macOS 開發 - NSMicrophoneUsageDescription (10.14 許可權問題)
阿新 • • 發佈:2018-11-01
文章目錄
升級 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]];
}