How to turn ON location services through Code - iphone

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:
}

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.

CLLocation Manager (How Enable "Use current Location")

I am trying to access user current Location.When i call CLLocationManager(startUpdating).
Its show alertview ex. "Xyz App would like to use your current location".
If user taps "Dont allow". and now if i check authorization status it reply 2 "means Denied".
I check status and if its 2(means denied), i show showalertview "Xyz want to use your current location"
Now if user Taps "Allow" how to enabled it programmatically,instead of user goes to setting screen & manually enabled.
You can not enable programmatically it the user taps don't allow. You just can inform the user that he has to go to settings->Location Services and enable it for your app if he wants to use this feature.
You can set the purpose of the location and it will be displayed in the first alert view that is displayed by the os when will ask user for permission.
purpose
An application-provided string that describes the reason for using location services.
#property(copy, nonatomic) NSString *purpose
Discussion
If this property is not nil and the system needs to ask for the user’s consent to use location services, it displays the provided string. You can use this string to explain why your application is using location services.
You must set the value of this property prior to starting any location services. Because the string is ultimately displayed to the user, you should always load it from a localized strings file.

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.

How to prompt user to turn on Location Services...again

I want to have the same functionality as the Map app, where user is prompted every time they press the 'current location' button to turn on their Location Services if they are off:
Turn off location services
User presses 'getCurrentLocation' button
App tries to get location using CLLocationManager
User gets 'Turn On Location Services..." message that shows "Settings" and "Cancel" buttons.
User taps 'Cancel'
User presses ''getCurrentLocation' button again
App tries to get location using CLLocationManager again
User does not get 'Turn On Location Services..." message any more
In the Map app, the user gets "Turn On Location Services..." message every time. How can I get my app to do the same? I made user I am using a new instance of CLLocationManager, in case that was the problem, but it was not. I can't see any settings that would affect this.
If I make my own Alert I cannot get the same 'Settings' button functionality. Also, I don't want the user to see multiple Alerts that look the same.
Any ideas?
New in iOS 8 there is a constant called UIApplicationOpenSettingsURLString.
From the "What's new in iOS" document under UIKit is the line:
You can take the user directly to your app-related settings in the Settings app. Pass the UIApplicationOpenSettingsURLString constant to the openURL: method of the UIApplication class.
From Apple's documentation:
UIApplicationOpenSettingsURLString
Used to create a URL that you can pass to the openURL: method. When you open the URL built from this string, the system launches the Settings app and displays the app’s custom settings, if it has any.
You can pass this into the UIApplication openURL: method. It might look something like:
NSURL *settings = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:settings])
[[UIApplication sharedApplication] openURL:settings];
If you want to point the user back to the Location Services screen in the Settings app, you can do so by sending them to a special URL, like so:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"prefs:root=LOCATION_SERVICES"]];
You can query the shared CLLocationManager instance if the location service is enabled. The correct way is to respect the users choice to disable location services.
But if you want to, just start the location service anyway and the user will be prompted to start it again. If the user opts in on the request locations will begin to be reported on your delegate as usual. If the user instead denies your request you will get a failure callback to the locationManager:didFailWithError: delegate method. The error will have an error code of kCLErrorDenied.
I would strongly discourage you from doing this, but you can try to start the service again if the user says no, and the user will be asked again. Most users will hate you for it though.

check if a user allows the app to use their location

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";
}
}