user875234
user875234

Reputation: 1

How do you request permissions for iOS?

I know the response to this question is "set it in the info.plist" but if I understand the way this works then if the user rejects the permissions you are requesting they don't get prompted again which effectively bricks your app unless you ...write code to check for permissions. Is that right? So I don't know why everyone acts like it's just automatic on iOS. You still have to check for permissions, right?

Assuming I am right, where can I find documentation on how to do it correctly. I checked this (solution was plugin I don't want to use), this (code is too complicated), this (long-winded non-answer), and this plus a few others from google which point to this plugin which I don't want to use. I just want a link to the documentation on how to check and request permissions on iOS. Is there such a link?

What I have looks like this:

private bool HasLocationPermission()
{
    return  CoreLocation.CLLocationManager.Status == CoreLocation.CLAuthorizationStatus.Authorized ||
            CoreLocation.CLLocationManager.Status == CoreLocation.CLAuthorizationStatus.AuthorizedAlways ||
            CoreLocation.CLLocationManager.Status == CoreLocation.CLAuthorizationStatus.AuthorizedWhenInUse;
}

but, of course, that is just for the "Location" permission. I don't see any information about what to check for in the LocationManager documentation. There's something about request rational or something? Where can I find how to do this? No plugins please.

Upvotes: 0

Views: 4903

Answers (1)

Cheesebaron
Cheesebaron

Reputation: 24470

If you study the code in the Permissions Plugin you linked to you can pretty easily deduct what you have to do.

In iOS there are 2 different types of location permissions, for either of them to work, you need to set up some descriptions in your Info.plist, which will be shown when prompted the permission dialog.

Set up either NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription depending on your needs. The distinction between this mode is. When in use, is for when occasionally needing location services, i.e. for briefly showing a map. While always is typically for apps needing it to track the users location all the time.

The section in the Info.plist will look something like:

<key>NSLocationWhenInUseUsageDescription</key>
<string>Do you want My App to access your location?</string>

As you've already figured out you can use CoreLocation and CLLocationManager to get the current permission status:

var locationManager = new CLLocationManager();
var status = locationManager.Status;

Status will either be:

  • NotDetermined - the App doesn't have permission yet or maybe never asked for it.
  • AuthorizedAlways - if you requested Always location and added the key NSLocationAlwaysUsageDescription.
  • AuthorizedWhenInUse - if you requested When In Use location and added the key NSLocationWhenInUseUsageDescription
  • Denied - user said "no thank you"

If you want to request the permission, simply call:

locationManager.RequestAlwaysAuthorization(); // for always
locationManager.RequestWhenInUseAuthorization(); // for when in use

You can listen to Authorization changes with the AuthorizationChanged event:

locationManager.AuthorizationChanged += OnAuthorizationChanged;

The CLAuthorizationChangedEventArgs will provide you the new status. You may want to hook up this even before requesting the permission.

private void OnAuthorizationChanged(object sender, CLAuthorizationChangedEventArgs args)
{
    if (args.Status == CLAuthorizationStatus.AuthorizedAlways ||
        args.Status == CLAuthorizationStatus.AuthorizedWhenInUse)
    {
       // all green, you are good to start listening to location changes!
    }
}

Now you can start listening to location changes:

locationManager.LocationsUpdated += OnLocationsUpdated;
locationManager.StartUpdatingLocation();

Upvotes: 4

Related Questions