Skip to content
This repository has been archived by the owner on Apr 3, 2023. It is now read-only.

Permission plugin version 6.0.1 crashes the app #180

Open
PraveenPotla opened this issue May 22, 2020 · 2 comments
Open

Permission plugin version 6.0.1 crashes the app #180

PraveenPotla opened this issue May 22, 2020 · 2 comments

Comments

@PraveenPotla
Copy link

If you are creating an issue for a BUG please fill out this information. If you are asking a question or requesting a feature you can delete the sections below.

Failure to fill out this information will result in this issue being closed. If you post a full stack trace in a bug it will be closed, please post it to http://gist.github.com and then post the link here.

Bug Information

Version Number of Plugin:6.0.1
Device Tested On:Android Version 9
Simulator Tested On:
Version of VS: 16.5.5
Version of Xamarin: Xamarin Version 16.5.00 and Xamarin.AndroidSDK 10.2
Versions of other things you are using:

Steps to reproduce the Behavior

I Install the recent plugin from NuGet 6.0.1 and xamarin. essentials version 1.5.3.2
Request calendar permission

Expected Behavior

Calendar permission set and application continues

Actual Behavior

I am getting below error and application crashes
System.MissingMethodException: 'Method not found: Android.App.Activity Xamarin.Essentials.Platform.get_CurrentActivity()'

Code snippet

async void ButtonPermission_OnClicked(object sender, EventArgs e)
{
if (busy)
return;

        busy = true;
        ((Button)sender).IsEnabled = false;

        var status = PermissionStatus.Unknown;
        switch (((Button)sender).StyleId)
        {
            case "Calendar":
                status = await CrossPermissions.Current.CheckPermissionStatusAsync<CalendarPermission>();
                break;
            case "Camera":
                status = await CrossPermissions.Current.CheckPermissionStatusAsync<CameraPermission>();
                break;
            case "Settings":
                CrossPermissions.Current.OpenAppSettings();
                ((Button)sender).IsEnabled = true;
                busy = false;
                return;
        }

        await DisplayAlert("Pre - Results", status.ToString(), "OK");

        if (status != PermissionStatus.Granted)
        {
            switch (((Button)sender).StyleId)
            {
                case "Calendar":
                    status = await Utils.CheckPermissions(new CalendarPermission());
                    break;
                case "Camera":
                    status = await Utils.CheckPermissions(new CameraPermission());
                    break;               
            }

            await DisplayAlert("Results", status.ToString(), "OK");

        }

        busy = false;
        ((Button)sender).IsEnabled = true;
    }

Utils Code:
public static async Task CheckPermissions(BasePermission permission)
{
var permissionStatus = await permission.CheckPermissionStatusAsync();
bool request = false;
if (permissionStatus == PermissionStatus.Denied)
{
if (Device.RuntimePlatform == Device.iOS)
{

				var title = $"{permission} Permission";
				var question = $"To use this plugin the {permission} permission is required. Please go into Settings and turn on {permission} for the app.";
				var positive = "Settings";
				var negative = "Maybe Later";
				var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
				if (task == null)
					return permissionStatus;

				var result = await task;
				if (result)
				{
					CrossPermissions.Current.OpenAppSettings();
				}

				return permissionStatus;
			}

			request = true;

		}

		if (request || permissionStatus != PermissionStatus.Granted)
		{
			permissionStatus = await permission.RequestPermissionAsync();


			if (permissionStatus != PermissionStatus.Granted)
			{
				var title = $"{permission} Permission";
				var question = $"To use the plugin the {permission} permission is required.";
				var positive = "Settings";
				var negative = "Maybe Later";
				var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
				if (task == null)
					return permissionStatus;

				var result = await task;
				if (result)
				{
					CrossPermissions.Current.OpenAppSettings();
				}
				return permissionStatus;
			}
		}

		return permissionStatus;
	}

Main activity Code

protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);
       
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        LoadApplication(new App());
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }

Screenshots

No

@jamesmontemagno
Copy link
Owner

Interesting....

Can you add:

var test = Xamarin.Essentials.Platform.CurrentActivity;

after

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);

@skycloudnest
Copy link

I agree, on await CrossPermissions.Current.RequestPermissionAsync(); invoked i Have ios app crash

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants