Home > other >  I can't use the Xamarin.Essentials RequestPermissionsAsync
I can't use the Xamarin.Essentials RequestPermissionsAsync

Time:01-27

I am trying to request the user of a mobile app the necessary permissions to access the contacts, but when calling the Xamarin.Essentials method, it tells me that Xamarin.Essentials.Permissions does not have a definition for RequestPermissionsAsync

I tried:

var status = await Xamarin.Essentials.Permissions
    .RequestPermissionsAsync(Permission.ContactsRead);

CodePudding user response:

Have you tried:

var status = await Permissions.RequestAsync<Permissions.ContactsRead>();

Unless you have an older version of Xamarin Essentials, the latest documentation of Xamarin.Essentials: Permissions would also agree that there is no such thing as RequestPermissionsAsync.

CodePudding user response:

It looks like you are using the correct method and namespace for requesting contacts permission with Xamarin.Essentials. However, it seems that the issue might be caused by one of the following reasons:

  1. You might not have the latest version of Xamarin.Essentials installed in your project. Make sure that you have the latest version of Xamarin.Essentials installed in your project.
  2. You might be missing a reference to the Xamarin.Essentials assembly in your project. Make sure that you have added a reference to the Xamarin.Essentials assembly in your project.
  3. The feature you are trying to use is not supported on the current platform. The Xamarin.Essentials.Permissions namespace provides a cross-platform way of requesting permissions, but not all features are available on all platforms. Make sure that you are checking for the availability of the feature on the current platform before attempting to use it.

    var status = await Xamarin.Essentials.Permissions.CheckStatusAsync(Permission.Contacts);
    if (status != PermissionStatus.Granted) {
        status = await Xamarin.Essentials.Permissions.RequestAsync(Permission.Contacts);
    }
  • Related