jamesmontemagno / PermissionsPlugin

Check and Request Permissions Plugin for Xamarin and Windows
MIT License
282 stars 85 forks source link

Exception with Xamarin.Essentials #127

Open vd3d opened 5 years ago

vd3d commented 5 years ago

Hi,

I try to use Xamarin.Essentials, and mainly the following code:

await CheckPermissions(Permission.LocationWhenInUse);
var request = new GeolocationRequest(GeolocationAccuracy.Best);
return await Geolocation.GetLocationAsync(request);

Before, I check the permissions with this method, no dialog appear and I got a "granted" permission. The problem is that I got an exception:

Xamarin.Essentials.PermissionException: 'LocationWhenInUse was not granted.'

What is strange is that I got this exception in (UWP x86) but not in (UWP x64) !!!!

Here is my code:

static async private Task CheckPermissions(Permission permission)
        {
            try
            {
                var status = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
                if (status != PermissionStatus.Granted)
                {
                    if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(permission))
                    {
                        //await NavigationHelper.CurrentPage.DisplayAlert("Need location", "Gunna need that location", "OK");
                    }

                    var results = await CrossPermissions.Current.RequestPermissionsAsync(permission);
                    //Best practice to always check that the key exists
                    if (results.ContainsKey(permission))
                        status = results[permission];
                }

                if (status == PermissionStatus.Granted)
                {
                    //var results = await CrossGeolocator.Current.GetPositionAsync(10000);
                    //LabelGeolocation.Text = "Lat: " + results.Latitude + " Long: " + results.Longitude;
                }
                else if (status != PermissionStatus.Unknown)
                {
                    await NavigationHelper.CurrentPage.DisplayAlert("Location Denied", "Can not continue, try again.", "OK");
                }
            }
            catch (Exception ex)
            {

                //LabelGeolocation.Text = "Error: " + ex;
            }
        }
mostafa901 commented 4 years ago

yep, same issue here... any advise?