Reputation: 4306
I am new in Xamarin.Forms. I want to ask for the location permission to the user, then if user allows it, get his location. The problem is RequestPermissionAsyn
and GetLastKnownLocationAsync
, both are async operations. Hence I want to wait till user grants permission and then call the GetLastKnownLocationAsync
. I am using this plugin for Permision Plugin.Permissions v3.0.0.12
Here is the code I am using
async void AskPermission(object sender, EventArgs e)
{
await Task.Run(async () =>
{
var permissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
if (permissionStatus != PermissionStatus.Granted)
{
var response = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location);
var userResponse = response[Permission.Location];
Debug.WriteLine($"Permission {Permission.Location} {permissionStatus}");
}
else
Debug.WriteLine($"Permission is finally {Permission.Location} {permissionStatus}");
});
// Call the GetLocation if user allowed the permission.
}
async void GetLocation()
{
Location location;
location = await Geolocation.GetLastKnownLocationAsync();
Location.Text = location.Latitude.ToString();
}
I can successfully prompt the user for allowing permission, but GetLocation()
is not executing. That means even if I write Debug.WriteLine("");
whether permission granted?, I am not getting anything in the output Window. Any Help would be much appreciated. Basically I want to run both the asynchronous operations one after the other. I do not understand whether the problem is with asynchronous operations or whether it needs to run from UI Thread.
Upvotes: 0
Views: 2445
Reputation: 4306
I think there might be a problem with RequestPermissionsAsync
not returning. Hence I have made this modification in my code. Now I ask the user permission using same plugin, and check if the the permission is enabled in other thread. This operation is performed every 1 second until the app gets required permission. When permission is allowed for the app to access location, dispose the timer, hence it will not check for the permissions again.
Also there is another bug in this used plugin inside the project. (Plugin.Permissions v3.0.0.12). When we ask for permission 2 times, and both the times if user denies the permission, then Exception was thrown. Hence I caught in inside try-catch
This is the working code, someone stuck here might find it useful:
public MainPage()
{
InitializeComponent();
Device.BeginInvokeOnMainThread(() => AskPermission());
CheckPermissionStatusRepeatedly();
...
}
async void AskPermission()
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
if (status != PermissionStatus.Granted)
{
await Application.Current.MainPage.DisplayAlert("Permission Request", "This app needs to access device location. Please allow access for location.", "Ok");
try
{
await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Location });
}
catch (Exception ex)
{
Location.Text = "Error: " + ex;
}
}
}
void CheckPermissionStatusRepeatedly()
{
Timer timer = null;
timer = new Timer(new TimerCallback(async delegate
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
if (status != PermissionStatus.Granted)
Debug.WriteLine("Still permission is not Granted");
else
{
Debug.WriteLine("Now permision is Granted, Hence calling GetLocation()");
Device.BeginInvokeOnMainThread(() => GetLocation());
timer.Dispose();
}
}), "test", 1000, 1000);
}
async void GetLocation()
{
Location location;
location = await Geolocation.GetLastKnownLocationAsync();
Location.Text = "Lat: " + location.Latitude + " Long: " + location.Longitude;
}
Upvotes: 3