项目场景:
程序在 macOS 环境下尝试访问麦克风时发生崩溃。
问题描述
崩溃信息如下:
bash
-------------------------------------
Translated Report (Full Report Below)
-------------------------------------
Process: test [2720]
Path: /Applications/test.app/Contents/MacOS/test
Identifier: hs.test Version: ???
Code Type: X86-64 (Native)
Parent Process: launchd [1]
User ID: 501
Date/Time: 2025-10-21 09:08:40.5221 +0800
OS Version: macOS 12.7.6 (21H1320)
Report Version: 12
Anonymous UUID: 4AEF0814-8866-A5DC-9EE9-7BC72539EBA0
Termination Reason: Namespace TCC, Code 0 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.
原因分析:
从最后一句描述可以看出,macOS(跟 iOS 类似)要求任何访问隐私相关硬件(如摄像头、麦克风、位置等)的应用,在 Info.plist 文件中声明用途描述。如果没有声明,系统会直接拒绝访问并触发崩溃。
解决方案:
在 test.app/Contents/Info.plist 中添加以下键值:
bash
<key>NSMicrophoneUsageDescription</key>
<string>此应用需要访问麦克风以进行音频采集或语音对讲。</string>
如果程序还需要使用摄像头(比如视频通话或监控),要同时加上:
bash
<key>NSCameraUsageDescription</key>
<string>此应用需要访问摄像头以进行视频预览或录像。</string>
如果是已打包的 .app,可以通过命令行修改:
bash
/usr/libexec/PlistBuddy -c "Add :NSMicrophoneUsageDescription string '此应用需要访问麦克风以进行音频采集或语音对讲。'" /Applications/test.app/Contents/Info.plist