iOS check if application has access to microphone

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
    }

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.


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
}

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