[FIXED] Xamarin Essentials Permissions.RequestAsync does not return

Issue

When calling Permissions.RequestAsync<Permissions.LocationWhenInUse>() I do not get any result and the App is getting stuck. No matter, if the user accepts or denies the request.

I’ve set up MainActivity as described in the documentation:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
    Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

Now I’m trying to get the permissions the following way. But the RequestAsync call is just getting stuck and I never recieve an answer. Once the permission is granted and the app is being restarted, everything works as expected.

public async Task CheckPermissions()
{
    var status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();

    if (status != PermissionStatus.Granted)
    {
        status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
    }
    

    if (status == PermissionStatus.Granted)
    {
        HasLocationPermissions = true;
    }
    else if (status != PermissionStatus.Unknown)
    {
        HasLocationPermissions = false;
    }
}

Solution

A synchronous blocking call further up your call stack can cause this; e.g., .Wait(), .Result, or .GetAwaiter().GetResult(). This is the classic deadlock situation described on my blog.

Since you are writing a Xamarin application, you may find this technique helpful – it’s a way to start an asynchronous operation (synchronously), show a "loading…" state, and then update when the asynchronous operation completes.

Answered By – Stephen Cleary

Answer Checked By – Marilyn (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published