0

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?

Sreejith Sree
  • 3,055
  • 4
  • 36
  • 105

1 Answers1

1

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.

Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22
  • My ios version is 14.3 – Sreejith Sree Jan 14 '21 at 15:47
  • Add the line `using Plugin.Permissions; using Plugin.Permissions.Abstractions;` – Lucas Zhang Jan 15 '21 at 06:32
  • Added the using statements, still an error: Error CS0104 'PermissionStatus' is an ambiguous reference between 'Plugin.Permissions.Abstractions.PermissionStatus' and 'Xamarin.Essentials.PermissionStatus' – Sreejith Sree Jan 15 '21 at 06:36
  • `using PermissionStatus = Plugin.Permissions.Abstractions.PermissionStatus;` Showing this as a suggestion, should I add it? – Sreejith Sree Jan 15 '21 at 06:38
  • Yes , add it and the error will disappear . – Lucas Zhang Jan 15 '21 at 06:39
  • Same result. This code is also working fine in android but not showing the permission alert in ios (both simulator and real device) – Sreejith Sree Jan 15 '21 at 08:17
  • I have added 2 keys in info.plist: `Privacy - Location Always Usage Description` and `Privacy - Location When In Use Usage Description`. I need location details when the app is in the foreground or background. – Sreejith Sree Jan 15 '21 at 08:21
  • My problem is the location permission alert(https://i.stack.imgur.com/MgAbm.png) is not showing and the location permission entry is not listing on the app properties page(https://i.stack.imgur.com/mD3Lw.png). – Sreejith Sree Jan 15 '21 at 08:26
  • Re-install the app and test again . – Lucas Zhang Jan 15 '21 at 08:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227373/discussion-between-lucas-zhang-msft-and-sreejith-sree). – Lucas Zhang Jan 15 '21 at 08:34