I have an application that uses the CoreLocation API. If the Location services is turned off , when i launch the application, It shows an alert saying that "Turn on Location Services " with "Settings" and "Cancel" Button. When the user Taps on Cancel , application should pop the current ViewController. Is there any way to handle when the user taps on "Cancel"?
Call +[CLLocationManager authorizationStatus]. It returns a value described here (under CLAuthorizationStatus). Also, check +[CLLocationManager locationServicesEnabled], which returns YES if location services are enabled.
You should use these methods before you call any CoreLocation method. Prompt the user accordingly.
You need to allocate CLLocationManager and handle +(CLAuthorizationStatus)authorizationStatus accordingly.
You can either call it from your AppDelegate or from you r ViewController.
Related
When calling the requestWhenInUseAuthorization method of CLLocationManager, if the user has disabled Location Services, iOS displays an alert that says:
Turn On Location Services to Allow "Your App Name" to Determine Your
Location
This alert has a Settings button that takes the user to "Location Services" in the Settings app, and a Cancel button.
This alert is great, but the problem I'm having is that it only displays up to 2 times. If the user clicks Cancel both times, then the next time I call requestWhenInUseAuthorization, no alert is displayed.
I would like to at least tell the user what the problem is in this scenario, but I don't know how to detect this scenario.
You have to save the number of authorization requests yourself.
The simplest way would be to use UserDefaults. Like this:
Save value:
UserDefaults.standard.set(true, forKey: "DidRequestAuthorization")
Load value:
let didRequestAuthorization = UserDefaults.standard.bool(forKey: "DidRequestAuthorization")
Then if didRequestAuthorizationis true you can display an alert next time that warns the user to cancel the request.
This alert is great, but the problem I'm having is that it only displays up to 2 times. If the user clicks Cancel both times, then the next time I call requestWhenInUseAuthorization, no alert is displayed.
As you rightly observe, you cannot rely on the system alert. But you don't have to. You can tell whether the user has disabled core location completely by calling the CLLocationManager class method locationServicesEnabled. You can then present your own 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
Hello I am using here UIWebView and load request for map . Now I have all done successfully but when I run app first time then Pop up show, I am attaching image here
Now I want to ask how to access this pop up actions (Don't allow and oK) ,because this is by default generated first time when we app run. I don't know how to access "Ok" button I want to write some code on "Ok" button.
So please anyone tell me how to access these pop up buttons in my case.
Implement CLLocationManagerDelegate Protocol to get call back event for that alert button pressed by authorization status.
locationManager:didChangeAuthorizationStatus:
Tells the delegate that the authorization status for the application changed.
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
There are various authorization status provided mention as follows :
typedef enum {
kCLAuthorizationStatusNotDetermined = 0,
kCLAuthorizationStatusRestricted,
kCLAuthorizationStatusDenied,
kCLAuthorizationStatusAuthorized,
} CLAuthorizationStatus;
kCLAuthorizationStatusNotDetermined
The user has not yet made a choice regarding whether this app can use location services.
kCLAuthorizationStatusRestricted
This app is not authorized to use location services. The user cannot change this app’s status, possibly due to active restrictions such as parental controls being in place.
kCLAuthorizationStatusDenied
The user explicitly denied the use of location services for this app or location services are currently disabled in Settings.
kCLAuthorizationStatusAuthorized
This app is authorized to use location services.
You can edit the message that is shown on UIAlertView, but you can not access the control of ok button, because iOS don't provide a delegate to access this button.
The alert is only shown for the first time, you launch the app and not after that.
You can only control the condition to present this alert but you cannot control the ok button
I've got an iOS app in which it starts differently if the user has been logged in through Facebook account or not.
So the application flow is as follows:
1- I call app delegate, which creates a navigationController and shows it.
2- In the root view controller, it checks if the user is logged in or not. By default (for example during the first boot) it loads view controllers as not logged in, showing only contents for not logged user. if the user is logged with Facebook account it sends requests to a server and shows the contents for logged in user. The requests start with didupdatelocation delegated method, from which it gets the current location.
3- there are many places in which the app asks if you want to log in. If the user gets correct login, it creates a new navigation controller, as in app delegate, and displays it. The problem is that in this way it doesn't call the method didupdatelocation, and so it doesn't get current location and doesn't make any request to server.
How can you suggest me to solve the problem?
Your design should not rely on didUpdateLocation to be called. This method is called at non-predictable intervals by the system.
One way to force it to call however, is to stop the locationManager and start it again.
startUpdatingLocation
stopUpdatingLocation
However, I recommend you consult the CLLocationManager Class Reference and re-design your login check accordingly.
If we try to access the user's location, iOS will tell the user that our application wants to use their location.
If I do this
[locationManager startUpdatingLocation];
An alert will show.
However, that only happens once.
It looks like some variable or default must have been set up once that display pops out.
How do I reset those default so that next time my app wants to use location users will be asked again?
Google map can displays that again and again.
It's Apple that asks them for permission, not you
Translation: You don't have any control over that part of the process. The little popup:
is only shown by Apple when you first ask for it - so the user always feels in control. After they accept for the first time, Apple assumes they are OK with your app getting their location information from their device, and won't show it again*.
*Unless they specifically go into Settings and disable Location Services for you app.
It's only showed on the first time and there's nothing you can do to change that. What you can do is ask your users to allow it on settings.
You can check if your app has permissions by checking:
[CLLocationManager locationServicesEnabled]
From the docs:
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. If this method returns NO and you start location updates anyway, the Core Location framework prompts the user to confirm whether location services should be reenabled.