即使获得了许可,iOS 麦克风选项也不在应用程序设置中

yun*_*yun 5 objective-c ios

我在 iOS 10 上请求麦克风许可时遇到了一个奇怪的问题。我放置了正确的 plist 属性(隐私 - 麦克风使用说明)并通过代码启用它。在我的手机上,麦克风工作/启用,我在手机的应用程序设置中看到它。但是,在朋友的手机上,麦克风会请求许可,但麦克风选项不会显示在应用程序的设置中。即使我正确设置了权限,我是否在这里遗漏了什么?为什么我的手机会在设置中显示该选项,而不是我朋友的手机?我有一部 iPhone SE,我的朋友有一部 iPhone 6s。

plist 属性:

<key>NSMicrophoneUsageDescription</key>
<string>Used to capture microphone input</string>
Run Code Online (Sandbox Code Playgroud)

请求许可的代码:

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];
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

rma*_*ddy 4

你的代码不正确。您检查用户是否已经拥有权限。如果没有,您就无需请求许可。您只需显示一个警报,其中包含转到设置页面的选项。但是,如果您的应用从未请求使用麦克风的权限,则“设置”页面上不会有麦克风设置。

您需要实际请求许可的代码。我有以下代码用于处理麦克风权限:

+ (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;
    }

}
Run Code Online (Sandbox Code Playgroud)

您可以按如下方式调用它:

[whateverUtilClass checkMicrophonePermissions:^(BOOL allowed) {
    if (allowed) {
        [self configureMicrophone];
    }
}];
Run Code Online (Sandbox Code Playgroud)