In Plugin.Permissions there was a ShouldShowRequestPermissionRationaleAsync method. However, in Xamarin.Essentials Permissions API there is no such a method.
Add the following method:
c#
bool ShouldShowRequestPermissionRationaleAsync(Permission permission)
I want to display the permission rationale on Android devices. Using Plugin.Permissions it was possible. However, in Xamarin.Essentials this is not possible.
Currently there is no way to check if user tapped only Deny or Deny and checked Never show again. Depending on this condition there may be different logic for handling denied permissions.
@mgierlasinski
You probably mean: There is NO way... 馃槃
@Mikilll94
Yep, corrected :)
Looking at Permissions plugin, for Android it could be something like:
```c#
private bool ShouldShowRequestPermissionRationale
{
var permission = new TPermission();
var activity = Platform.CurrentActivity;
var names = permission.RequiredPermissions;
foreach ((string name, bool isRuntime) in names)
{
if (ActivityCompat.ShouldShowRequestPermissionRationale(activity, name))
return true;
}
return false;
}
```
@Redth
Do you know when Xamarin.Essentials 1.6 are going to be released? I am waiting for this feature.
I'm also interested in knowing more about this change and how long it'll take, before I spend a bunch of time trying to figure out how this will impact our app when moving to essentials from the permissions plugin, and doing a bunch of refactoring that'll be useless whenever this gets released.
@Mikilll94 I've implemented my own service to do this check - duplicating the code from the permissions plugin is not too hard. I can't hold up development in this area. When Essentials 1.6 is released I can remove it.
@Mikilll94 I've implemented my own service to do this check - duplicating the code from the permissions plugin is not too hard. I can't hold up development in this area. When Essentials 1.6 is released I can remove it.
Sounds good @Mikilll94 - any chance you could share the code you have added please?
@Redth can I ask why this issue was closed? Is the new API still planned to be added? I will need to go back to the old permission plugin if not. Thanks!
@AdamDiament
sorry I forgot to add my implementation
Made a ShowPermissionRationale service that exists on all three platforms.
public abstract class ShowPermissionRationale
{
public virtual bool ShouldShowRequestPermissionRationale(PermissionToCheck permissionToCheck) => false;
}
Returns false by default, except on android which has this overriding implementation.
Note: PermissionToCheck is just a basic enum I'm using elsewhere, you can pass along whatever you need to get the permission checks you need. this particular code is just adapted to my needs.
public class AndroidShowPermissionRationale : ShowPermissionRationale
{
public override bool ShouldShowRequestPermissionRationale(PermissionToCheck permissionToCheck)
{
if(Xamarin.Essentials.Platform.CurrentActivity is Android.App.Activity act)
{
var permissions = new List<string>();
switch (permissionToCheck)
{
case PermissionToCheck.Camera:
permissions.Add(Manifest.Permission.Camera);
break;
case PermissionToCheck.LocationWhenInUse:
permissions.Add(Manifest.Permission.AccessCoarseLocation);
permissions.Add(Manifest.Permission.AccessFineLocation);
break;
case PermissionToCheck.Storage:
permissions.Add(Manifest.Permission.ReadExternalStorage);
permissions.Add(Manifest.Permission.WriteExternalStorage);
break;
case PermissionToCheck.Photos:
default:
break;
}
foreach (var p in permissions)
{
if (ActivityCompat.ShouldShowRequestPermissionRationale(act, p))
{
return true;
}
}
}
return false;
}
}
@AdamDiament they merged 1.6 into main, getting ready for release, just a bit ago, probably why this got closed. see https://github.com/xamarin/Essentials/pull/1382
also to note that 1.6.0 will likely require Xamarin forms version 4.7 or higher, which is a rather large jump from 4.2 and not everyone will be capable of getting it straight away, so a workaround is a nice thing to have...
Most helpful comment
@Mikilll94
Yep, corrected :)
Looking at Permissions plugin, for Android it could be something like:() where TPermission : BasePlatformPermission, new()
```c#
private bool ShouldShowRequestPermissionRationale
{
var permission = new TPermission();
var activity = Platform.CurrentActivity;
var names = permission.RequiredPermissions;
}
```