Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:)
    withObject:permissionBlock];
}
#ifndef __IPHONE_7_0
  typedef void (^PermissionBlock)(BOOL granted);
#endif

PermissionBlock permissionBlock = ^(BOOL granted) {
  NSLog(@"permissionBlock");
  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?

like image 288
Nitya Avatar asked Jan 15 '14 16:01

Nitya


1 Answers

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) {
        NSLog(@"granted");
    } else {
        NSLog(@"denied");

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Microphone Access Denied"
                                                            message:@"You must allow microphone access in Settings > Privacy > Microphone"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
       [alert show];
    }
}];
like image 96
Matt Avatar answered Sep 20 '22 11:09

Matt