GPS location is on/off in iphone? - iphone

I want to do like, when the app start, app check for iPhone GPS ON or OFF? if GPS off then app quite.

What do you want exactly to know, location service or does user granted access to the location service?
If first, than depending on iOS version you can use:
on iOS pre 4.x:
CLLocationManager *testLocationManager = [[CLLocationManager alloc] init];
BOOL servicesEnabled = [testLocationManager locationServicesEnabled];
[testLocationManager release];
on iOS 4.x+:
BOOL servicesEnabled = [CLLocationManager locationServicesEnabled];
If second, than:
on iOS pre 4.x: there is no quick snippet, you have to wait if didFailWithError or didUpdateToLocation will be called and if second, than user granted access
on iOS 4.x+:
BOOL accessGranted = [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized;
And than easily use exit(0) if you option not reached. And yes, try notify user about what you is about to quit the app.

You need to create a CLLocationManager and ask it for locationServicesEnabled. This will tell you if location services are available.
Note however that Apple does not allow apps to quit on their own, the user needs to press the home button instead. If you discover location services are unavailable, you need to tell the user that your app needs them and they should be activated for your app to work properly.

Related

How to check GPS permission is on or off for specific our application in iOS?

with the use of
BOOL locaService=[CLLocationManager locationServicesEnabled];
it will return boolean value for general GPS service, How can we check the service is on or off for specific our application.
[CLLocationManager authorizationStatus] returns a CLAuthorizationStatus which is typedef'd to be kCLAuthorizationStatusNotDetermined, kCLAuthorizationStatusRestricted, kCLAuthorizationStatusDenied, or kCLAuthorizationStatusAuthorized.
kCLAuthorizationStatusAuthorized means you can use the GPS, and anything else means you can't.
(kCLAuthorizationStatusDenied means they have disallowed you, kCLAuthorizationStatusRestricted means you can't due to parental controls, kCLAuthorizationStatusNotDetermined means that the user has not yet responded to the "Allow this app to see my location?" prompt.)
Check authorizationStatus in CLLocationManager.
authorizationStatus
Returns the application’s authorization status for using location
services.
+ (CLAuthorizationStatus)authorizationStatus

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.

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.