Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Location permission issue iOS 11 and iOS 10

I am having an issue when requesting location permissions from user when I use iOS11 my info.plist contains

<key>NSLocationWhenInUseUsageDescription</key>
<string>When in use permissions</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>always permissions</string>
<key>NSLocationAlwaysAndWhenInUsageDescription</key>
<string>Always and in usage permissions</string>

I have two maps one for customer and another for employees. For employees I need to know their location even if the app is not running or is backgrounded (they are able to turn it off when signing out) and request permission using

locationManager.requestAlwaysAuthorization()

For customer i only need locations while the app is in use and request the permission using

locationManager.requestWhenInUseAuthorization()

In iOS 11 this only requests permission when in usage and never the always on permission.

In iOS 10 it has the correct behaviour.

The behaviour I want is as follows: When they are a customer (not signed in) it only asks for when in use permission. If they sign in (employee) it request location even when not in use.

If anyone can shed some light on what I am missing / done wrong it would be much appreciated.

Something to note if i remove the permission NSLocationAlwaysUsageDescription iOS10 and iOS11 have the same issue of not requesting always permission.

A bit more clarification. I have implemented didChangeAuthorization delegate function and it gets called when a users allow the permission from alert from calling requestWhenInUseAuthorization() however when I call requestWhenInUseAuthorization() function on location manager the delegate method is not called it is like it's never receiving that call and no alert dialog is shown to the user.

like image 467
Ben Avery Avatar asked Sep 25 '17 05:09

Ben Avery


People also ask

How do I set location permissions in Xcode?

Here we will be using when-in-use authorization: Request authorization to use location services only when your app is running. Step 1 − Open Xcode, Single View Application, name it LocationServices. Step 2 − Open the Main. storyboard and add one button and name it getLocation.

What is Cllocationmanager in Swift?

The object that you use to start and stop the delivery of location-related events to your app.


3 Answers

I figured out the issue by creating a quick stand alone app that only asked for permissions, I was given an error log that stated the keys I was using were wrong.

I had NSLocationAlwaysAndWhenInUsageDescription instead of NSLocationAlwaysAndWhenInUseUsageDescription which is odd because from the docs it states that NSLocationAlwaysAndWhenInUsageDescription should be used. Switching to include the correct key fixed issue and now permissions works as expected for iOS 11 and 10.

Thanks for all the help.

like image 139
Ben Avery Avatar answered Oct 04 '22 22:10

Ben Avery


In your info.plist file add this:

<key>NSLocationUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>

Now in your swift file, don't forget to add delegate: CLLocationManagerDelegate

In your viewDiDLoad(), add this:

locationManager = CLLocationManager()

locationManager.delegate = self

locationManager.requestWhenInUseAuthorization()

locationManager.desiredAccuracy = kCLLocationAccuracyBest

locationManager.startUpdatingLocation()

locationManager.startMonitoringSignificantLocationChanges()

// Here you can check whether you have allowed the permission or not.

if CLLocationManager.locationServicesEnabled()
    {
        switch(CLLocationManager.authorizationStatus())
        {

        case .authorizedAlways, .authorizedWhenInUse:

            print("Authorize.")

            break

        case .notDetermined:

            print("Not determined.")

            break

        case .restricted:

            print("Restricted.")

            break

        case .denied:

            print("Denied.")
        }
    }
like image 34
Akhil Nair Avatar answered Oct 04 '22 22:10

Akhil Nair


For both cases, customers and employees, you first need to call locationManager.requestWhenInUseAuthorization()

Then, only if they are employees, add a call to locationManager.requestAlwaysAuthorization()

See https://developer.apple.com/documentation/corelocation/choosing_the_authorization_level_for_location_services/request_always_authorization

Overview To configure always authorization for location services, do the following: Add the NSLocationWhenInUseUsageDescription key and the NSLocationAlwaysAndWhenInUsageDescription key to your Info.plist file. (Xcode displays these keys as "Privacy - Location When In Use Usage Description" and "Privacy - Location Always and When In Use Usage Description" in the Info.plist editor.) If your app supports iOS 10 and earlier, add the NSLocationAlwaysUsageDescription key to your Info.plist file. (Xcode displays this key as "Privacy - Location Always Usage Description" in the Info.plist editor.) Create and configure your CLLocationManager object. Call the requestWhenInUseAuthorization() initially to enable your app's basic location support. Call the requestAlwaysAuthorization() method only when you use services that require that level of authorization.

like image 24
Roee84 Avatar answered Oct 04 '22 21:10

Roee84