一、問題
升級 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]];
}
五、刪除權限請求記錄
請求過一次權限,就會在隱私面板有記錄;
如果本次已授權,把電腦上的這個 App 全刪光,下次安裝,還是已授權狀態;
全刪除後,再次安裝,不會彈出系統請求接口;
如果需要彈出這個窗口,可以使用 tccutil 命令。
如:
$ tccutil reset Microphone
六、參考
mojave 權限問題可參考:
-
Mojave 的隱私設置和重置權限方法
https://www.vastiny.com/post/ji-zhu/reset-microphone -
Technical Q&A QA1906 : Resetting Privacy Settings in iOS and macOS
https://developer.apple.com/library/archive/qa/qa1906/_index.html