Nitya Nitya - 1 year ago 76
Objective-C Question

How to check for microphone access at time of launch?

In my app, I will be using a microphone to do some recording. From iOS7.0 onwards, the user is asked to check the permission to access the microphone before starting the audio.

I have a button 'Start Recording' in my app. Here it first checks the user's permission for recording.

Here's the code to do this:

if([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)]) {
[[AVAudioSession sharedInstance] performSelector:@selector(requestRecordPermission:)
#ifndef __IPHONE_7_0
typedef void (^PermissionBlock)(BOOL granted);

PermissionBlock permissionBlock = ^(BOOL granted) {
if (granted) {
[self doActualRecording];
} else {
// Warn no access to microphone

Now, I want to ask the user to authorize microphone use as the user starts the app. Then when the user selects Record button, it gives a popup message again.

A similar functionality happens with Location services. How can I do this for microphone access?

Answer Source

Once a user has denied microphone access for your app, you cannot present them with the permissions dialog again. The saved settings are used. Instead you can prompt the user to go into their settings and make the change.

[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
    if (granted) {
    } else {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Microphone Access Denied"
                                                            message:@"You must allow microphone access in Settings > Privacy > Microphone"
       [alert show];