在Xamarin中阅读iOS的相机权限

Sak*_*gra 6 xamarin.ios ios xamarin ios-camera

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

如果应用程序没有访问摄像头的权限,我现在需要这样做.我需要检查是否为相机授予了权限并相应地显示消息.我怎样才能做到这一点?

Sak*_*gra 5

我有答案.这是我做的:

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


Roo*_*elt 2

你检查过这个答案吗? 在 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();
}
Run Code Online (Sandbox Code Playgroud)