How to detect user giving microphone permission on iOS?
Asked Answered
N

2

6

So the thing is that I need to call some function after user gives (or declines) a permission to use the microphone.

I already saw this:

[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
        if (granted) {
            // Microphone enabled code
            [self someFunction];

        }
        else {
            // Microphone disabled code
        }
 }];

However, this works only to detect current state.

If the current state is "no" and popup shows and user gives the permission - the function will not be called. That's because in the moment of executing this the permission was "no" and until we run the code next time the function will not be called.

What I want to do is to call a function after the user pressed either "allow" or "decline".

Anyone knows how to do this?

EDIT: Forgot to mention it has to be iOS 7.0 up compatible solution.

Nil answered 11/10, 2015 at 12:49 Comment(1)
I don't think that you can detect the selection of the automatic record permissions, however, you can create a timer that checks if the app has permissions every 1 second or so, and if it does, the timer stops and the app goes about it's purpose as expected.Jena
G
12

A method of AVAudioSession introduced in iOS 8 is recordPermission. This returns an enum named AVAudioSessionRecordPermission. You could use a switch to determine whether the permission alert has been presented to the user or not. This way you only call requestRecordPermission when it has not been presented to the user, so the permission block can assume it is being executed after the user allows or disallows permission for the first time.

An example would be something like -

AVAudioSessionRecordPermission permissionStatus = [[AVAudioSession sharedInstance] recordPermission];

switch (permissionStatus) {
     case AVAudioSessionRecordPermissionUndetermined:{
          [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
          // CALL YOUR METHOD HERE - as this assumes being called only once from user interacting with permission alert!
              if (granted) {
                  // Microphone enabled code
              }
              else {
                  // Microphone disabled code
              }
           }];
          break;
          }
     case AVAudioSessionRecordPermissionDenied:
          // direct to settings...
          break;
     case AVAudioSessionRecordPermissionGranted:
          // mic access ok...
          break;
     default:
          // this should not happen.. maybe throw an exception.
          break;
}
Grime answered 11/10, 2015 at 13:55 Comment(2)
This looks like I good answer but I forgot to mention that I need iOS 7.0 up compatible solution. I'll up vote the solution for the effort.Nil
I see... since the permissions alert is only presented to the user one time, maybe consider writing some sort of flag to NSUserDefaults after you call requestRecordPermission. But check for the flag before calling and if it's not present then you'll know it's being called for the first time and therefor you can make a similar assumption in the permission block as my answer.Grime
W
0

If use has not yet given your permission, do the following:

  1. First, show the popup dialogue
  2. Run your code in OP

-

if([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission)]) {
    [[AVAudioSession sharedInstance] requestRecordPermission];
    // Now run your function
}
Wart answered 11/10, 2015 at 13:3 Comment(3)
I think OP means that he/she wants to capture the automatic record request that iOS pops up for you when you're trying to access the microphone.Jena
I have no idea what OP is nor where this code should be executed from. Tried to add it and the code executes right away so there is no way to capture user's responseNil
OP means "original poster". The code can be inserted inside a method. When the method is called this code is executed.Jena

© 2022 - 2024 — McMap. All rights reserved.