iOS check if application has access to microphone
Solution 1
In iOS7
there is no way to get the current status of microphone authorization
.They have given the enum in iOS8
as AVAudioSessionRecordPermission
In iOS7
you have to request permission every time with
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (granted) {
NSLog(@"Permission granted");
}
else {
NSLog(@"Permission denied");
}
}];
The same question has been asked before but there is no such api with which you know current status as in iOS8
You can refer Check for mic permission on iOS 7 without showing prompt
Solution:
Another option is you can show the popup
or ask for permission first time and save the states of user option selected in NSUserDefaults
and than onwards do not ask for permission.
From docs you explicitly do not need to call this if each you do not need to get the permission of user.It will automatically called by AVAudioSession
first time when you try to record
Recording audio requires explicit permission from the user. The first time your app’s audio session attempts to use an audio input route while using a category that enables recording (see “Audio Session Categories”), the system automatically prompts the user for permission; alternatively, you can call requestRecordPermission: to prompt the user at a time of your choosing
Solution 2
You can check the with recordPermission(), which has been available since iOS 8.
Keep in mind that starting with iOS 10, you must set the NSMicrophoneUsageDescription
property in your info.plist
for microphone permissions and include a message for the user. This message is shown to the user at time of the request. Finally, if localizing your app, be sure to include your plist strings for translation.
Failure to do so will result in a crash when attempting to access the microphone.
This answer has been cleaned up again for Swift 5.x
import AVFoundation
switch AVAudioSession.sharedInstance().recordPermission {
case .granted:
print("Permission granted")
case .denied:
print("Permission denied")
case .undetermined:
print("Request permission here")
AVAudioSession.sharedInstance().requestRecordPermission({ granted in
// Handle granted
})
@unknown default:
print("Unknown case")
}
Objective-C
I have tested this code with iOS 8 for the purpose of checking for microphone permission and obtaining the current state.
switch ([[AVAudioSession sharedInstance] recordPermission]) {
case AVAudioSessionRecordPermissionGranted:
break;
case AVAudioSessionRecordPermissionDenied:
break;
case AVAudioSessionRecordPermissionUndetermined:
// This is the initial state before a user has made any choice
// You can use this spot to request permission here if you want
break;
default:
break;
}
As always, make sure to import AVFoundation
.
Solution 3
Swift 3 Complete Solution Code
func checkMicPermission() -> Bool {
var permissionCheck: Bool = false
switch AVAudioSession.sharedInstance().recordPermission() {
case AVAudioSessionRecordPermission.granted:
permissionCheck = true
case AVAudioSessionRecordPermission.denied:
permissionCheck = false
case AVAudioSessionRecordPermission.undetermined:
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
if granted {
permissionCheck = true
} else {
permissionCheck = false
}
})
default:
break
}
return permissionCheck
}
Solution 4
There is another way you can try following code for ios 7 and 8 :
let microPhoneStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeAudio)
switch microPhoneStatus {
case .Authorized:
// Has access
case .Denied:
// No access granted
case .Restricted:
// Microphone disabled in settings
case .NotDetermined:
// Didn't request access yet
}
Solution 5
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (granted) {
// Microphone enabled code
}
else {
// Microphone disabled code
}
}];
And include <AVFoundation/AVAudioSession.h>
lukas
Updated on October 29, 2021Comments
-
lukas over 2 years
With the introduction of iOS 7, applications have to request microphone access when they want to record audio.
How do I check if the application has access to the microphone?
In the iOS 8 SDK I can use theAVAudioSessionRecordPermission
enum, but how do I check this in iOS 7?Info:
I don't want to request permission, I just want to check if the app has access to the microphone. (Like Location access):if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) { // Do something }
-
lukas almost 10 yearsI don't want to request recording permission, I just want to check if the application has access to the microphone (like with location access:
[CLLocationManager authorizationStatus]
) -
souvickcse almost 10 yearsIt will request for the first time from next time onwards it will not ask again
-
lukas almost 10 yearsSo I can check Microphone access just in iOS 8?
-
Elijah about 6 yearsFYI, the property to add to your info.plist is NSMicrophoneUsageDescription
-
CodeBender about 6 years@Harris Thanks, my screenshot clipped it but I will make it more explicit
-
Martin Mlostek over 4 yearsthis will not work as
requestRecordPermission
is invoked async. There will be alwaystrue
returned -
user2899094 almost 4 yearsi tried this but it says property requestrecordpermission not found on object type avaudiosession
-
souvickcse almost 4 yearscan you share a dummy project?
-
Guilherme Crozariol over 3 yearsYou can use semaphores in objective-c or DispatchGroup in Swift, in this case you can wait for the result, then return it.