我正在开发一个 macOS 应用程序,我试图在不直接触发默认权限对话框的情况下访问麦克风。相反,我设法以编程方式打开系统设置,特别是
Privacy & Security -> Microphone section
,允许用户手动授予权限。
但是,有一个问题。即使用户在系统设置中手动打开我的应用程序的麦克风权限后,
AVCaptureDevice.authorizationStatus(for: .audio)
仍然返回.notDetermined
。
为了澄清,我避免使用
AVCaptureDevice.requestAccess(for: .audio)
,因为它会提示默认权限对话框。但是当我使用它时,该应用程序可以正确识别权限状态的变化。仅当尝试检测直接从系统设置进行的权限更改时才会出现问题。
这是我的代码:
struct SystemSettingsHandler {
static func openSystemSetting(for type: String) {
guard type == "microphone" || type == "screen" else {
return
}
let microphoneURL = "x-apple.systempreferences:com.apple.preference.security?Privacy_Microphone"
let screenURL = "x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture"
let urlString = type == "microphone" ? microphoneURL : screenURL
guard let url = URL(string: urlString) else { return }
NSWorkspace.shared.open(url)
}
}
private func requestMicrophonePermission(completion: @escaping (Bool) -> Void) {
switch AVCaptureDevice.authorizationStatus(for: .audio) {
case .authorized:
print("authorized")
completion(true)
case .notDetermined:
print("notDetermined")
AVCaptureDevice.requestAccess(for: .audio) { granted in
if granted {
completion(granted)
} else {
completion(granted)
}
}
case .denied, .restricted:
print("denied")
SystemSettingsHandler.openSystemSetting(for: "microphone")
completion(false)
@unknown default:
print("unknown")
completion(false)
}
}
感谢您提前提供的帮助。
毕竟我还没有找到这个问题的答案..我很好奇!!!!!!!!!