8

I am requesting the user location in my app.

locationManager.requestAlwaysAuthorization()

This line of code should return this alert message:

enter image description here

However, I want the user to choose between "Don't Allow" and "Always Allow" only. how can I delete the "Only While Using The App" option?

Knowing that this is what I included in the info.plist, and deleting any row from these 3 rows in the info.plist is causing the app not to show this alert at all.

enter image description here

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
mahdi
  • 439
  • 3
  • 15

3 Answers3

9

It is not optional anymore.

Since iOS 11 has been released, if your application requests the location to be always on (locationManager.requestAlwaysAuthorization()), the users will automatically be given all three options.

Unlike in previous iOS versions, all options have to be displayed to the user. That leads to: you have to add a key for both options.

Adapted from Apple's Article - Requesting Always Authorization:

You are required to include the NSLocationWhenInUseUsageDescription and NSLocationAlwaysAndWhenInUseUsageDescription keys in your app's Info.plist file. (If your app supports iOS 10 and earlier, the NSLocationAlwaysUsageDescription key is also required.) If those keys are not present, authorization requests fail immediately.

Reference: What's New in Location Technologies Video Session.

Pang
  • 9,564
  • 146
  • 81
  • 122
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
3

According to WWDC 2017 Session 713 about updates in Location technologies, you should always include When In Use description when linking against iOS 11 and later, if you want to request Always authorization. And iOS will additionally display an option to use location for When-In-Use mode.

Quote from session transcript.

So for iOS 11 we're asking all developers with Always requesting apps to additionally support the WhenInUse authorization mode. This change is both retroactive and forward-looking, so when the user updates to iOS 11, they will be able to authorize any app that requests Always authorization the WhenInUse authorization mode instead. If you link against the iOS 11 SDK, you must provide a WhenInUseUsageDescription. Otherwise, your app will be unable to request Always authorization. Furthermore, when your app requests Always authorization, we will additionally display an option to grant your app WhenInUse authorization instead. With these new three option prompts we have a new UsageDescription string as well. Your app needs to provide an AlwaysAndWhenInUse UsageDescription. Since this is a different key your app must provide it when you link against the new iOS 11 SDK. For apps linked against iOS 11 and later, Core Location will not use the old NSLocationAlways UsageDescription key.

0

When are not set you will catch a message in the debug console like:

The app's Info.plist must contain both NSLocationAlwaysAndWhenInUseUsageDescription and NSLocationWhenInUseUsageDescription keys with string values explaining to the user how the app uses this data

  1. Open Info.plist as Source Code enter image description here

  2. And add the following xml chunk:

<key>NSLocationAlwaysUsageDescription</key>
<string>Location always usage description</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Location always and when in usage description</string>
<key>NSLocationUsageDescription</key>
<string>Location usage desription</string>