在 Xamarin 中读取 iOS 的相机权限

Read camera permission for iOS in Xamarin

我有一个用 Xamarin 开发的 iOS 应用程序。当应用程序没有访问麦克风的权限时,如果用户尝试从应用程序访问麦克风,我会使用 AVAudioSession.SharedInstance().RequestRecordPermission (delegate(bool granted)) 检查设置并显示一条消息。

现在如果应用没有访问相机的权限,我需要做同样的事情。我需要检查是否授予相机权限并相应地显示消息。我该怎么做?

你检查过这个答案了吗? Detect permission of camera in iOS 我认为这就是您正在寻找的解决方案:)。

编辑: 这是移植到 C#

的最高投票答案的代码
// replace the media type to whatever you want
AVAuthorizationStatus authStatus = AVCaptureDevice.GetAuthorizationStatus(AVMediaType.Video);
switch (authStatus)
{
    case AVAuthorizationStatus.NotDetermined:
        break;
    case AVAuthorizationStatus.Restricted:
        break;
    case AVAuthorizationStatus.Denied:
        break;
    case AVAuthorizationStatus.Authorized:
        break;
    default:
        throw new ArgumentOutOfRangeException();
}

我得到了答案。这是我所做的:

AVCaptureDevice.RequestAccessForMediaType (AVMediaType.Video, (bool isAccessGranted) => {                    
   //if has access              
   if(isAccessGranted)
   {
      //do something
   }
   //if has no access
   else
   {
      //show an alert
   }
});