Forum Xamarin.Forms

how to display multiple permissions at once in xamarin.forms

DevMaranDevMaran Member ✭✭
edited May 2019 in Xamarin.Forms

I want to show runtime permission popup for camera and location when the app starts or after splash screen. I am using the NuGet package: https://github.com/jamesmontemagno/PermissionsPlugin

I tried the below code on button click event.

private async void Button_Clicked(object sender, EventArgs e)
    {
        await GetPermissions();
    }


public static async Task<bool> GetPermissions()
    {
        bool permissionsGranted = true;

        var permissionsStartList = new List<Permission>()
        {
            Permission.Location,
            Permission.LocationAlways,
            Permission.LocationWhenInUse,
            Permission.Storage,
            Permission.Camera
        };

        var permissionsNeededList = new List<Permission>();
        try
        {
            foreach (var permission in permissionsStartList)
            {
                var status = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
                if (status != PermissionStatus.Granted)
                {
                    permissionsNeededList.Add(permission);
                }
            }
        }
        catch (Exception ex)
        {
        }

        var results = await CrossPermissions.Current.RequestPermissionsAsync(permissionsNeededList.ToArray());

        try
        {
            foreach (var permission in permissionsNeededList)
            {
                var status = PermissionStatus.Unknown;
                //Best practice to always check that the key exists
                if (results.ContainsKey(permission))
                    status = results[permission];
                if (status == PermissionStatus.Granted || status == PermissionStatus.Unknown)
                {
                    permissionsGranted = true;
                }
                else
                {
                    permissionsGranted = false;
                    break;
                }
            }
        }
        catch (Exception ex)
        {
        }
        return permissionsGranted;
    }

Answers

Sign In or Register to comment.