Request permissions not working within PermissionsPlugin

For my Xamarin.Forms-App I'm using the PermissionsPlugin v2.0.1.

If no permission is granted, I want to request the user to do so by CrossPermissions.Current.RequestPermissionsAsync(Permission.Storage)

The problem is that the function always returns Unknown instead of requesting the user for the permission. One time, it worked...but I don't know what I changed that it's not working anymore..

Here my code:

MainActivity.cs (Android-project)

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

AndroidManifest.xml

<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="25" />
<uses-permission android:name="android.permission.CAMERA" />

PermissionsExample.xaml.cs

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PermissionExample
{
    public PermissionExample()
    {
        this.InitializeComponent();
    }

    private async void Button_OnClicked(object sender, EventArgs e)
    {
        var requestedPermissions = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Camera);
        var requestedPermissionStatus = requestedPermissions[Permission.Camera];

        await this.DisplayAlert("", $"Permission: {requestedPermissionStatus}", "ok");
    }
}

Thanks for your help! :)

Best Answer

Answers

  • tschennietschennie USMember ✭✭

    Anyone? :smile:

  • gdkgdk INMember ✭✭✭

    Hi Tschennie, Here is the code you can try for asking user permissions manually. Here you can write what permissions do you want. I have wrote some sample permission in this sample code. You can call 'RequestPermissionsManually()' method in main activity it' working fine.

          private void RequestPermissionsManually()
        {
            try
            {
                if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessFineLocation) != Permission.Granted)
                    _permission.Add(Manifest.Permission.AccessFineLocation);
    
                if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.Camera) != Permission.Granted)
                    _permission.Add(Manifest.Permission.Camera);
    
                if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != Permission.Granted)
                    _permission.Add(Manifest.Permission.WriteExternalStorage);
    
                if(ContextCompat.CheckSelfPermission(this, Manifest.Permission.CallPhone) != Permission.Granted)
                    _permission.Add(Manifest.Permission.CallPhone);
    
    
                if(_permission.Count > 0)
                {
                    string[] array = _permission.ToArray();
    
                    RequestPermissions(array, array.Length);
                }
    
            }
            catch(Exception ex)
            {
                LoggingManager.Error(ex);
            }
        }
    
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
        {
            if (requestCode == 2)
            {
                if (grantResults.Length == _permission.Count)
                {
                    for (int i = 0; i < requestCode; i++)
                    {
                        if (grantResults[i] == Permission.Granted)
                        {
    
                        }
                        else
                        {
                            _permission = new List<string>();
                            RequestPermissionsManually();
                            break;
                        }
                    }
                }
    
            }
        }
    
  • tschennietschennie USMember ✭✭

    @Malcolm said:
    Hi Tschennie,
    I was having exactly the same problem until I saw the post by @SuatKorkmaz dated April 19 here:
    h ttps://forums.xamarin.com/discussion/56571/permissions-plugin-for-xamarin-simplifying-runtime-permissions/p1. Just add the following line in the OnCreate method of your MainActivity

    CrossCurrentActivity.Current.Activity = this;

    Thank you very much! You saved my day :smile:

  • SuatKorkmazSuatKorkmaz USMember ✭✭✭

    :) Good to hear that.

  • dtelarolidtelaroli Member

    You also saved my day :smile:

  • VipinKrishnaVipinKrishna Member ✭✭

    @Malcolm said:
    Hi Tschennie,
    I was having exactly the same problem until I saw the post by @SuatKorkmaz dated April 19 here:
    h ttps://forums.xamarin.com/discussion/56571/permissions-plugin-for-xamarin-simplifying-runtime-permissions/p1. Just add the following line in the OnCreate method of your MainActivity

    CrossCurrentActivity.Current.Activity = this;

    Thanks...

  • AbsarAbsar Member

    You also saved my day. :smile:

  • Amit_Malhi01Amit_Malhi01 Member ✭✭

    CrossCurrentActivity.Current.Activity = this;
    Worked for me

  • AditkothariAditkothari USMember ✭✭

    @Malcolm said:
    Hi Tschennie,
    I was having exactly the same problem until I saw the post by @SuatKorkmaz dated April 19 here:
    h ttps://forums.xamarin.com/discussion/56571/permissions-plugin-for-xamarin-simplifying-runtime-permissions/p1. Just add the following line in the OnCreate method of your MainActivity

    CrossCurrentActivity.Current.Activity = this;

    Working perfectly ..Thanks for the help

  • @Malcolm said:
    Hi Tschennie,
    I was having exactly the same problem until I saw the post by @SuatKorkmaz dated April 19 here:
    h ttps://forums.xamarin.com/discussion/56571/permissions-plugin-for-xamarin-simplifying-runtime-permissions/p1. Just add the following line in the OnCreate method of your MainActivity

    CrossCurrentActivity.Current.Activity = this;

    Thank you so much.. You saved my day!!! :)

  • SuatKorkmazSuatKorkmaz USMember ✭✭✭

    I'm glad to help.

  • HarshitaHarshita INMember ✭✭✭✭

    Permission Plugin

Sign In or Register to comment.