40

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 the AVAudioSessionRecordPermission 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
}
1

8 Answers 8

85

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.

enter image description here

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.

2
  • FYI, the property to add to your info.plist is NSMicrophoneUsageDescription
    – Elijah
    Mar 7, 2018 at 4:44
  • 1
    @Harris Thanks, my screenshot clipped it but I will make it more explicit
    – CodeBender
    Mar 7, 2018 at 18:31
33

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

0
13

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
    }
2
  • this will not work as requestRecordPermission is invoked async. There will be always true returned Oct 7, 2019 at 9:33
  • You can use semaphores in objective-c or DispatchGroup in Swift, in this case you can wait for the result, then return it. Nov 17, 2020 at 19:58
11

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
}
5
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
        if (granted) {
            // Microphone enabled code
        }
        else {
            // Microphone disabled code
        }
    }];

And include <AVFoundation/AVAudioSession.h>

5
  • 1
    I 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])
    – lukas
    Jul 27, 2014 at 13:29
  • 1
    It will request for the first time from next time onwards it will not ask again
    – souvickcse
    Jul 27, 2014 at 13:31
  • So I can check Microphone access just in iOS 8?
    – lukas
    Jul 27, 2014 at 13:34
  • i tried this but it says property requestrecordpermission not found on object type avaudiosession Jun 18, 2020 at 17:44
  • can you share a dummy project?
    – souvickcse
    Jun 18, 2020 at 20:45
5

What I often end up doing for a quick check on objects working with audio record:

// swift 5 
static public func isAuthorized() -> Bool {
    return AVCaptureDevice.authorizationStatus(for: .audio) == .authorized
}
4

Since none of the other answers here mentioned this, you need to add the permissions to your info.plist. Specifically, add an entry for:

Privacy - Microphone Usage Description

For the String value, enter something like: (App name) needs access to your microphone.

Otherwise, you get a mysterious crash

3

import AVFoundation and use the following function

var permissionCheck:Bool = false

switch AVAudioSession.sharedInstance().recordPermission {

        case AVAudioSession.RecordPermission.granted:
            permissionCheck = true

        case AVAudioSession.RecordPermission.denied:
            permissionCheck = false
        case AVAudioSession.RecordPermission.undetermined:
            AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
                if granted {
                    permissionCheck = true
                } else {
                    permissionCheck = false
                }
            })
        default:
            break
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.