5
votes

I'm having a weird issue with asking for permission to the microphone on iOS 10. I put the proper plist property (Privacy - Microphone Usage Description) and enabling it through code. On my phone, the microphone works/enabled and I see it in my phone's settings for the app. However, on a friend's phone, the microphone asks for permission but then the microphone option doesn't show up in the app's settings. Am I missing something here even though I set the permissions correctly? Why would my phone show the option in the settings but not my friend's phone? I have an iPhone SE and my friend has an iPhone 6s.

plist property:

<key>NSMicrophoneUsageDescription</key>
<string>Used to capture microphone input</string>

code asking for permission:

if ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio] == AVAuthorizationStatusAuthorized) {
    [self configureMicrophone];
}
else {
    UIAlertController *deniedAlert = [UIAlertController alertControllerWithTitle:@"Use your microphone?"
                                                                         message:@"The FOO APP requires access to your microphone to work!"
                                                                  preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"Go to Settings" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action){
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }];
    [deniedAlert addAction:action];
    [self presentViewController:deniedAlert animated:YES completion:nil];
}

Thanks!

1
Why are people down voting a perfectly good question? The question is clear, and it provides relevant details. What else do you want?rmaddy

1 Answers

4
votes

Your code isn't correct. You check to see if the user already has permission. If it doesn't, you don't ask for permission. You simply show an alert with an option to go to the settings page. But there won't be a microphone setting on the Settings page if your app never requests permission to use the microphone.

You need code that actually requests permission. I have the following code I use for dealing with microphone permission:

+ (void)checkMicrophonePermissions:(void (^)(BOOL allowed))completion {
    AVAudioSessionRecordPermission status = [[AVAudioSession sharedInstance] recordPermission];
    switch (status) {
        case AVAudioSessionRecordPermissionGranted:
            if (completion) {
                completion(YES);
            }
            break;
        case AVAudioSessionRecordPermissionDenied:
        {
            // Optionally show alert with option to go to Settings

            if (completion) {
                completion(NO);
            }
        }
            break;
        case AVAudioSessionRecordPermissionUndetermined:
            [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
                if (granted && completion) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        completion(granted);
                    });
                }
            }];
            break;
    }

}

You can call this as follows:

[whateverUtilClass checkMicrophonePermissions:^(BOOL allowed) {
    if (allowed) {
        [self configureMicrophone];
    }
}];