Question

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?

Was it helpful?

Solution

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];
    }
}];

OTHER TIPS

I had the same question. See this response to a related question including sample code for checking and handling permission status using AVAudioSession which you can customize to provide the user experience you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top