我在请求 iOS 10 上的麦克风许可时遇到了一个奇怪的问题。我放置了正确的 plist 属性(隐私 - 麦克风使用说明)并通过代码启用它。在我的手机上,麦克风工作/启用,我在手机的应用程序设置中看到它。但是,在 friend 的手机上,麦克风会请求许可,但麦克风选项不会显示在应用程序的设置中。即使我正确设置了权限,我是否在这里遗漏了什么?为什么我的手机会在设置中显示该选项,而我 friend 的手机却不会?我有一部 iPhone SE,我的 friend 有一部 iPhone 6s。
plist 属性:
<key>NSMicrophoneUsageDescription</key>
<string>Used to capture microphone input</string>
请求权限的代码:
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];
}
谢谢!
最佳答案
您的代码不正确。您检查用户是否已经拥有权限。如果没有,则您不会请求许可。您只需显示一个带有转到设置页面的选项的警报。但是,如果您的应用从不请求使用麦克风的权限,则“设置”页面上不会有麦克风设置。
您需要实际请求权限的代码。我有以下代码用于处理麦克风权限:
+ (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;
}
}
你可以这样调用它:
[whateverUtilClass checkMicrophonePermissions:^(BOOL allowed) {
if (allowed) {
[self configureMicrophone];
}
}];
关于即使已获得许可,iOS 麦克风选项也不在应用程序设置中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40966171/