Retrieve Authorization Status for Core Location - ios

Apple has deprecated the authorizationStatus method. Is there a way to know which is the status of the authorisation for my App?
Let's say that a user granted the permission in a previous session and with another app session I wanted to show or hide a button depending on the fact that the user has granted the permission or not. How can I get this information now that authorizationStatus is no longer available?

You should use locationManagerDidChangeAuthorization delegate.

see documentation: CLLocationManager:
class func authorizationStatus() -> CLAuthorizationStatus
is deprecated.
there are new instance property that available since iOS 14
var authorizationStatus: CLAuthorizationStatus - The current
authorization status for the app.

Related

requestAlwaysAuthorization after requestWhenInUseAuthorization is accepted

My app can have two level of LocationHandler status. First, I launch requestWhenInUseAuthorization and then, if the user activates some specific features, I launch requestAlwaysAuthorization.
I need to be notified if user refuses the requestAlwaysAuthorization to let him know the feature won't work as expected. The problem is that in this case didChangeAuthorizationStatus is not called because the authorization status stays the same (it was AuthorizedWhenInUse and it's still AuthorizedWhenInUse).
Do you have any idea how I could be notified if user refuses AuthorizedAlways after accepting AuthorizedWhenInUse ?
since iOS 10 or so it is no longer possible to call requestAlwaysAuthorization() after you have called requestWhenInUseAuthorization() even if the user accepted when-in-use.
in prior versions (at least iOS8) you could "step up" the authorization and ask for always-authorization after the user accepted when-in-use. This is no longer possible.
best thing to do is check CLLocationManager.authorizationStatus()
once in a while and show a dialog pointing the user to the right settings page with UIApplicationOpenSettingsURLString

Handling cancel of the location services alert

I do have a situation where I need to get current location of iPhone 5 running iOS8. When the location services is off, the default location service alert open up. This alert has 'Cancel' and 'Settings' button. Please find below the screenshot of the alert.
I need to handle the situation when user pressed cancel on the alert. I can handle it when I override the alert. But that alert isn't working for me, as settings of that custom alert takes me to the app level location settings and not device level location settings.
Set the delegate for your CLLocationManager and implement the delegate's method
- locationManager:didChangeAuthorizationStatus:
This method is called whenever the application’s ability to use location services changes. Changes can occur because the user allowed or denied the use of location services for your application or for the system as a whole.
If the authorization status is already known when you call the requestWhenInUseAuthorization or requestAlwaysAuthorization method, the location manager does not report the current authorization status to this method. The location manager only reports changes to the authorization status. For example, it calls this method when the status changes from kCLAuthorizationStatusNotDetermined to kCLAuthorizationStatusAuthorizedWhenInUse.
Link to Apple doc

Beacons and User Location permission

I just finished developing an app that interacts with the Beacons and User location.
I ask for locationManager the requestAlwaysAuthorization permission and I have added in the plist NSLocationAlwaysUsageDescription property with my description; everything works perfectly!!!
I realized that: if a user does not accept the requested permission, iOS disables localization always and when in use, making very limited the use of the app.
I wish that if a user refuses the requestAlwaysAuthorization automatically being asked requestWhenInUseAuthorization permission!
This is possible with some native method or I have to handle the request for another permission?
Thanks to all!
EDIT:
How do apps like Shazam or Facebook to have three choices "Never," "When in use" and "Always" in the location settings?
Surely there is a way to present them to the user?!?!
You can't do that. When in doubt, please always head to the Apple documentation.
https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/requestAlwaysAuthorization
After requestAlwaysAuthorization is finished (the user accepted/denied), the status is changed to ether kCLAuthorizationStatusDenied or kCLAuthorizationStatusAuthorized(or some other, doesn't matter).
Furthermore, both requestAlwaysAuthorization and requestWhenInUseAuthorization both have such logic (described in the documentation)
If the current authorization status is anything other than kCLAuthorizationStatusNotDetermined, this method does nothing and does not call the locationManager:didChangeAuthorizationStatus: method.`
If the user denies the requestAlwaysAuthorization the status is changed to kCLAuthorizationStatusDenied and both request authorizations will be ignored in future.

Location Services enabled from native Settings App

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.

Change system pop-up in iPhone app for core-location permissions

Instead of system default below popup for core location permissions, how can I use my default pop up for my app?
And when does this popup come? Is it coming on particular delegate Method?
Is there any way to disable that?
You can not replace that UIAlertView but cutomize the text to hopefully explain better why your app needs access to the user's location.
See https://stackoverflow.com/questions/12562152/replacement-for-purpose-property-of-cllocationmanager
This popup comes when you access this locationServicesEnabled property of CLLocationManager.
[CLLocationManager locationServicesEnabled];
You can handle this in your app. But anyway, you cann't handle it first time according to apple's doc(see locationServicesEnabled topic.
locationServicesEnabled
Returns a Boolean value indicating whether location services are enabled on the device.
+ (BOOL)locationServicesEnabled
Return Value
YES if location services are enabled; NO if they are not.
Discussion
The user can enable or disable location services from the Settings application by toggling the Location Services switch in General.
You should check the return value of this method before starting
location updates to determine whether the user has location services
enabled for the current device. Location services prompts users the
first time they attempt to use location-related information in an app
but does not prompt for subsequent attempts. If the user denies the
use of location services and you attempt to start location updates
anyway, the location manager reports an error to its delegate.
After disallow, try to attempt,
if ([CLLocationManager locationServicesEnabled)
{
//Do your work
}
else
{
if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorized)
{ //Show Alert view... }
}

Resources