iOS – How to request permission?

Xamarin

AVAuthorizationStatus authStatus = AVCaptureDevice.GetAuthorizationStatus(AVAuthorizationMediaType.Video);
if (authStatus == AVAuthorizationStatus.Authorized)
{
    // Show camera
    ...
}
else if (authStatus == AVAuthorizationStatus.NotDetermined)
{
    Debug.WriteLine("Camera access not determined. Ask for permission.");
    AVCaptureDevice.RequestAccessForMediaType(AVAuthorizationMediaType.Video, (granted) =>
    {
        if (granted)
        {
            // Show camera
            ...
        }
        else
        {
            camDenied();
        }
    });
}
else if (authStatus == AVAuthorizationStatus.Restricted)
{
    // This device doesn't have the camera feature
}
else
{
    // Denied
    camDenied();
}

void camDenied()
{
    UIAlertController alert = UIAlertController.Create("Warning", "It looks like your privacy settings are preventing us from accessing your camera. You can fix this by doing the following...", UIAlertControllerStyle.Alert);

    UIAlertAction goAction = UIAlertAction.Create("Go", UIAlertActionStyle.Default, (action) =>
    {
        // Open Settings 
        UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));
    });
    alert.AddAction(goAction);

    PresentViewController(alert, true, null);
}

Be the first to comment

Leave a Reply

Your email address will not be published.


*