check if a user allows the app to use their location - iphone

The first time the app tries to get the users location they are prompted with "Would like to use your current location" and they can hit Don't allow or ok. Is there any way to find out if the user has hit ok or don't allow? I'm trying to have the MKMapView show the users current location but I would like to take different actions based on the users selection.
Normally you would think there would be a delegate to get this information but there doesn't seem to be.
Thanks in advance for your help.

Your first call to get the user's location will fail with an error which tells you the user has denied location services. Your CLLocationManagerDelegate method didFailWithError will be called, as below. (The constants are defined in CLError.h)
- (void)locationManager:(CLLocationManager*)aManager didFailWithError:(NSError*)anError
{
switch([anError code])
{
case kCLErrorLocationUnknown: // location is currently unknown, but CL will keep trying
break;
case kCLErrorDenied: // CL access has been denied (eg, user declined location use)
message = #"Sorry, flook has to know your location in order to work. You'll be able to see some cards but not find them nearby";
break;
case kCLErrorNetwork: // general, network-related error
message = #"Flook can't find you - please check your network connection or that you are not in airplane mode";
}
}

Related

why CLLocationManager always returns true?

I am using [CLLocationManager locationServicesEnabled] function to get the status whether location service is enabled or not. I kept code in viewDidLoad method.
if(![CLLocationManager locationServicesEnabled])
{
NSLog(#"No");
}
else
{
NSLog(#"Yes");
}
When I am running this app first time it returns Yes. Why? It should return me No. This is the case when I did not select "Allow" or "Don't allow" options. Means I neither allow nor don't allow but in viewDidLoad I got Yes.
Now I select "Don't allow" and again load the same viewController. At this time at least it should return me No, but still I got Yes. Why?
So much of confusion with CLLocationManager.
Any ideas?
locationServicesEnabled returns whether location service is enabled on settings.. If it is enabled in settings, this function returns YES all the time..
from documentation
locationServicesEnabled
Returns a Boolean value indicating whether location services are enabled on the device.
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. 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.
Whether or not user allowed/rejected app permission (in the alertview) doesn't affect the return value of this method.
If you want to know whether user has given application permission to access location, you can use authorizationStatus.

How to turn ON location services through Code

How to turn ON location services through Code in my application if location service is in OFF condition i want it to ON through alertView i have two buttons in alertview cancel & settings
if i click settings it needs to go settings of the device is it possible???
Could anyone help me??
thanks...........
You can't enable location services in code. You just invoke the service, and iOS will seek permission from the user on behalf of your app.
I've had to deal with the same problem but this is just not possible under iOS. You have to check if location services is enabled :
[CLLocationManager locationServicesEnabled];
and if not, ask the user to go to the settings to enable it (through an AlertView for example).
Note that Apple says :
locationServicesEnabled : You should check the return value of this
method before starting location updates to determine if 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 with a confirmation panel asking whether
location services should be reenabled.
But that only seem to work the very first time you check. If the user denies, the confirmation panel is not shown anymore.
You may also check :
switch ([CLLocationManager authorizationStatus]) {
case kCLAuthorizationStatusDenied:
case kCLAuthorizationStatusRestricted:
case kCLAuthorizationStatusNotDetermined:
}

Location service iOS alert call back

When we use location services in an application, we receive an iOS alert saying the application is trying to use the current location -- Allow/Don't Allow.
Do we have a delegate call back for these buttons?
I want to handle tap on "Don't Allow".
You don't have direct access to that alert.
If the user presses "Don't Allow", or if the app otherwise doesn't have permission to use location services then CLLocationManager will call locationManager:didFailWithError: on its delegate. The error domain will be kCLErrorDomain and the error code will be kCLErrorDenied.
You can simply get the action selected like below:
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
[self addRegion];
}
else if (status == kCLAuthorizationStatusDenied) {
NSLog(#"Location access denied");
}
}
make sure to set the delegate of location manager.
You should also check to see if the user has allowed location services for your app before starting the location manager. Use the CLLocationManager class method locationServicesEnabled to check.
Here's the doc:
locationServicesEnabled
Returns a Boolean value indicating whether location services are enabled on the device.
+ (BOOL)locationServicesEnabled
Return Value
YES if location services are enabled or NO if they are not.
Discussion
The user can enable or disable location services altogether from the Settings application by toggling the switch in Settings > General > Location Services.
You should check the return value of this method before starting location updates to determine if 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 with a confirmation panel asking whether location services should be reenabled.

"Don't Allow" in LocationManager keeps logging errors

I have an app that checked for location. It asks the user to use location and if the user says no on the menu there is an issue when i load the mapview.
Once i select the mapView it asks for the user location again. If the user says no again my console keeps displaying errors/warning as well as my NSLog from the "didFailWithError" of my location Manager class.
Is there a way of stopping the LocationManage:didFailWithErrors if the user has already said no? I don't think Apple would accept my app if the Log file gets filled up my the LocationManager
Here is an example of what gets repeated in the console
ERROR,Time,290362745.002,Function,"void CLClientHandleDaemonDataRegistration(__CLClient*, const CLDaemonCommToClientRegistration*, const __CFDictionary*)",server did not accept client registration 1
WARNING,Time,290362745.005,Function,"void CLClientHandleDaemonInvalidation(__CFMessagePort*, void*)",client 1035.0 has been disconnected from daemon
2010-03-15 12:19:05.002 SAQ[1035:207] LocationManager Error Denied by user
Documentation on -didFailWithError: method says:
If the user denies your application’s
use of the location service, this
method reports a kCLErrorDenied error.
Upon receiving such an error, you
should stop the location service.
So after receiving this error you should message you location manager to stop updating location:
[manager stopUpdatingLocation];
If you're using MKMapView I think setting its showsUserLocation property to NO should do the trick.
It is your responsibility to check the error code and stop updating the location if the error code is kCLErrorDenied.

How to get user response when prompted for core location?

When I install my application on the iPhone it asks for the current location with the options "Don't allow" and "Ok" in an alert. How do I find out which option was chosen? I also want to show this option only once. If the user chooses to allow their current location to be found, I want the device to automatically get the location in the background.
If user denied access to Location service then CLLocationManager delegate method didFailWithError: gets called:
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
if (error.code == kCLErrorDenied){
// User denied access to location service
}
}
Your controller should implement the CLLocationManagerDelegate protocol. This defines two methods that you will need to implement:
– locationManager:didUpdateToLocation:fromLocation:
In this method you put your code to handle location updates.
– locationManager:didFailWithError:
In this method you put you code to handle the user denying your request, or updates failing.
Once the user allows you to use their location, they won't be prompted again unless they exit the app. There isn't a way to prevent the phone from prompting users each time they start up the app though.