I'm developing a macOS app and I'm trying to access the microphone without directly triggering the default permission dialog. Instead, I've managed to programmatically open the System Settings, specifically the Privacy & Security -> Microphone section, allowing users to manually grant permission.
However, there's an issue. Even after the user manually toggles on the microphone permission for my app in System Settings, the AVCaptureDevice.authorizationStatus(for: .audio) still returns .notDetermined.
To clarify, I'm avoiding the use of AVCaptureDevice.requestAccess(for: .audio) because it prompts the default permission dialog. But when I do use it, the app correctly recognizes changes in permission status. The problem arises only when trying to detect permission changes made directly from the System Settings.
Here is my code
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
if let url = URL(string: urlString) {
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)
}
}
Thank you for reading this post!