I would like to requests the user’s permission to use location services while using the app, Allow Once or Don't allow.
enter image description here
I only have code in ViewController.swift because I build the app with Ruby on Rails.
Which property list key I should use and how?
You need to call locationManager for this:
locationManager.requestAlwaysAuthorization()
To request "always" location access as well as:
locationManager.requestWhenInUseAuthorization()
Related
I'm trying to request location "Always" but no matter my settings I only see three options:
Allow While Using App
Allow Once
Don't Allow
Here is my code:
// ViewController.swift
import UIKit
import CoreLocation
class ViewController: UIViewController {
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false
}
}
My Info.plist includes strings for these three:
Privacy - Location Always and When In Use Usage Description
Privacy - Location When In Use Usage Description
My app Capabilities has "location updates" turned on in Background Modes.
What am I missing here?
Edit: Removed an old Info.plist key/value pair that was deprecated.
iOS 13 Location Permissions
NSLocationAlwaysUsageDescription - deprecated.
This key is required if your iOS app uses APIs that access the user’s location at all times and deploys to targets earlier than iOS 11.
The Always Allow option, which grants an app “background” location
access, has been removed from the initial location permissions prompt.
Instead, users can select Keep Only While Using (aka “foreground”
permission) or select a new option,Allow Once. So how does an app then
get upgraded to “background” location permission? If the user
continues to use the app, iOS 13 will now automatically and
periodically prompt to upgrade location permissions from While Using
to Always Allow.
I have followed the apple developer guide on location services.
I have included the info.plist key value pair:
key: Privacy - Location Always Usage Description
value: The application myTestApp needs access to location services even in the background
I have created an instance of CLLocationManager as a class variable of the view controller:
var locationManager = CLLocationManager()
I have code in the view controller viewDidLoad() function that assigns the delegate and checks for the current status:
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
let status = CLLocationManager.authorizationStatus()
switch status {
case .denied, .restricted, .notDetermined, .authorizedWhenInUse :
print("The switch detected a state other than always")
locationManager.requestAlwaysAuthorization()
default:
print("Services Authorized")
}
Yet, when I build and run the application after making small changes it does not prompt for user access, and even worse, when it does sometimes work it thinks the user is in the middle of the Atlantic ocean at Lat: 0.0 and Long: 0.0.
Is there anything outside of the code I need to do so that I can recreate the user experience of authorizing the application and then seeing it zoom to a real location?
The user permission prompt is one time only. Once it is allowed,
device would fetch user location automatically from that point.
Try setting the custom location in the simulator via these two methods.
1. Using the simulator menu actions
2. Using the Xcode debugger options
The simulator handles location a bit differently from the device. I'd suggest pushing the app over to the device itself and running from there to see how it asks for permission and make sure it is giving the right location. It's very easy to do and very helpful for testing with apps that use access to location services.
https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/LaunchingYourApponDevices/LaunchingYourApponDevices.html
Essentially, plug phone in, select it from the list of devices to simulate with, press play and accept your developer access on the phone to run the app from your device.
I am using the google google places api or an iOS app and was wondering how to avoid having to ask for allowing "always on" location (NSLocationAlwaysUsageDescription). I do not think my app needs to access the location while in the background (is there a reason to?)
My google places api call is in a callback in viewDidLoad. Should I put it elsewhere in order to only ask for less intrusive permissions?
Thanks
There are 2 methods on CLLocationManager that request the location access. In order to request location access while the app is running you need to call requestWhenInUseAuthorization.
For example:
let locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
I'm assuming you were following this guide where it show location access like this:
locationManager.requestAlwaysAuthorization()
That method does request the access to always use your device's location, even while the app is in the background.
Hope that helps!
When my app is launched it requests to use location services.
If a user selects 'Don't Allow' I prompt again letting them know that Location Services are required for the best experience and they can enable in the settings app.
If a user does not allow and still creates an account, the main screen will not fully function without the location feature part.
From this point, if I manually enable in the Settings app I'm still not getting the main page to pick up the current location.
How do I detect that location services have been enabled from the Settings App?
Is there a method I need to enforce again from the AppDelegate?
You can tell if location has been enabled for your app using CLLocationManager.AuthorizationStatus, which returns a member of the CLAuthorizationStatus enum. If location is disabled completely, your app won't be authorized, so you know everything you need to know.
let authorization = CLLocationManager.authorizationStatus()
if authorization == .AuthorizedWhenInUse || authorization == .Authorized {
...
}
If you request authorization using CLLocationManager and the user denies it, you can't cause the window to come up again.
From a UX point of view, be careful about nags as well. Communicate clearly that your app benefits from using location, but try to avoid browbeating the user about it.
See also how to determine when settings change on ios to have your app retry location access right after a user (hopefully) enabled it.
In android, you define permissions for gps, sms sending, location , .., in the manifest file.
Is there anything similar in the iOS, so the user would know what capabilities of the phone some app uses before installation?
Or is the user warned during app use when some function wants to use something (e.g. gps, sms...)?
In iOS you declare your application requirements in its manifest-like Info.plist. But this information is not used to ask user permission, only for ensuring device compatibility.
Only Notifications and Location Services require user permission, which is automatically asked to the user the very first time your application attempt to use the corresponding API.
My guess is that many other permissions are already granted via the Apple Store license agreement, that the user must have accepted, unlike Android (I guess you can install an app without using the market isnt? which changes a lot from a legal point of view)
There's no such things as permissions on iPhone.
The only thing that user is warned about is when application uses his current location - then user is prompted with system alert and must explicitly allow or deny application's access to location data.
What concerns sms and email, they can be created and sent only via standard controllers so user will be aware of that anyway
on iOS you don't have to declare all necessary permissions in some specific file. You just use them. For example location
info.plist
//Privacy - Location When In Use Usage Description
<key>NSLocationUsageDescription</key>
<string>title</string>
//Privacy - Location Always and When In Use Usage Description
<key>NSLocationWhenInUseUsageDescription</key>
<string>description</string>
import CoreLocation
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
}
[IDFA]