2016-12-05 151 views
1

我在向iOS 10上的麦克风要求权限时遇到了一个奇怪的问题。我将适当的plist属性(隐私 - 麦克风使用说明)码。在我的手机上,麦克风可以工作/启用,并且可以在手机的应用程序设置中看到它。但是,在朋友的电话上,麦克风要求许可,但话筒选项不会显示在应用程序的设置中。我在这里错过了什么,即使我正确设置了权限?为什么我的手机会在设置中显示选项,但不是我朋友的手机?我有一个iPhone SE,我的朋友有一个iPhone 6s。iOS麦克风选项即使在授予权限的情况下也不在应用程序设置中

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

谢谢!

+2

为什么人们反对投票一个非常好的问题吗?问题很清楚,并提供了相关的细节。你还想要什么? – rmaddy

回答

3

您的代码不正确。您检查用户是否已经有权限。如果没有,你不要求许可。您只需显示一个提示即可进入设置页面。但是如果您的应用永远不会请求使用麦克风的权限,则设置页面上不会有麦克风设置。

您需要实际请求权限的代码。我有以下的代码我用对付麦克风权限:

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

好吧,这是有道理的,但为什么它出现在我的手机? –

+0

我能想到的唯一一点是,您的应用程序曾确实要求获得许可,并且已在您的设备上触发。 – rmaddy

+0

啊我认为这是有道理的。谢谢! –