Sreejith Sree
Sreejith Sree

Reputation: 3716

Xamarin forms: Location permission alert is not showing in iPhone device

I have added the location permission feature in my project and the permission alert is working fine on ios simulators. But in a real iPhone device, permission is not asking.

I have added the location permissions in the info.plist file like below.

enter image description here

The app settings page shows all the other permission details except the location.

enter image description here

My Code:

public async void ShareLocation()
{
    var status = await Permissions.RequestAsync<Permissions.LocationAlways>();
    if (status == PermissionStatus.Granted)
    {
        //Actions
    }
}

Reference: Xamarin Forms: How to check if GPS is on or off in Xamarin ios app?

Upvotes: 0

Views: 980

Answers (1)

Lucas Zhang
Lucas Zhang

Reputation: 18861

In iOS 13, Apple made a big changes in location permission’s behaviour, especially for Always Allow permission.

In Xamarin.Forms you could use the plugin Permissions Plugin from nuget .

Usage

try
{
    var status = await CrossPermissions.Current.CheckPermissionStatusAsync<LocationPermission>();
    if (status != PermissionStatus.Granted)
    {
        if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
        {
            await DisplayAlert("Need location", "Gunna need that location", "OK");
        }

        status = await CrossPermissions.Current.RequestPermissionAsync<LocationPermission>();
    }

    if (status == PermissionStatus.Granted)
    {
        //Query permission
    }
    else if (status != PermissionStatus.Unknown)
    {
        //location denied
    }
}
catch (Exception ex)
{
  //Something went wrong
}

By the way , what is the iOS version on your real device ? The key Privacy - Location Always and When In Use Usage Description is available after iOS 10.0 .Use this key if your iOS app accesses location information while running in the background. If your app only needs location information when in the foreground, use NSLocationWhenInUseUsageDescription instead.

Upvotes: 1

Related Questions