Location permission alert is not showing in android in net maui

anil kumar 61 Reputation points
2023-03-19T17:13:52.1233333+00:00

I am using below code on xaml page but location permission in not working with promt method


 protected async override void OnAppearing()
    {
        base.OnAppearing();

var status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();

         if (status == PermissionStatus.Granted) 

        {
            //some code to access location 
        }
        if (Permissions.ShouldShowRationale<Permissions.LocationWhenInUse>())
        {
            await Shell.Current.DisplayAlert("Needs permissions", "BECAUSE!!!", "OK");
        }

        status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
        if (status != PermissionStatus.Granted)
            await Shell.Current.DisplayAlert("Permission required",
                "Location permission is required for bluetooth scanning. " +
                "We do not store or use your location at all.", "OK");
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,747 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 33,381 Reputation points Microsoft Vendor
    2023-03-20T05:34:41.29+00:00

    Hello,

    After my testing, the method if (status != PermissionStatus.Granted) after clicking on deny overrides the method in if (Permissions.ShouldShowRationale<Permissions.LocationWhenInUse>()).

    For requesting Location permissions, there are out-of-the-box examples in the official example, please refer to the documentation and code snippets:

    public async Task<PermissionStatus> CheckAndRequestLocationPermission()
    {
        PermissionStatus status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
       if (status == PermissionStatus.Granted)
            return status;
       if (status == PermissionStatus.Denied && DeviceInfo.Platform == DevicePlatform.iOS)
        {
            // Prompt the user to turn on in settings
            // On iOS once a permission has been denied it may not be requested again from the application
            return status;
        }
       if (Permissions.ShouldShowRationale<Permissions.LocationWhenInUse>())
        {
            // Prompt the user with additional information as to why the permission is needed
            await Shell.Current.DisplayAlert("Needs permissions", "BECAUSE!!!", "OK");
        }
       status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
       return status;
    }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments