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
}
CodeBender
  • 35,668
  • 12
  • 125
  • 132
lukas
  • 2,300
  • 6
  • 28
  • 41

8 Answers8

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.

CodeBender
  • 35,668
  • 12
  • 125
  • 132
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

Community
  • 1
  • 1
codester
  • 36,891
  • 10
  • 74
  • 72
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
    }
mriaz0011
  • 1,887
  • 23
  • 11
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
}
lukas
  • 2,300
  • 6
  • 28
  • 41
user3873271
  • 111
  • 1
  • 2
5
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
        if (granted) {
            // Microphone enabled code
        }
        else {
            // Microphone disabled code
        }
    }];

And include <AVFoundation/AVAudioSession.h>

souvickcse
  • 7,742
  • 5
  • 37
  • 64
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
}
itMaxence
  • 1,230
  • 16
  • 28
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

Hugh
  • 443
  • 4
  • 11
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
        }
vrat2801
  • 538
  • 2
  • 13