CLLocation Manager (How Enable "Use current Location") - iphone

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.

Related

Swift how to set manually photoLibrary and camera authorization?

When using my app, the user is asked permission to access camera and library one time (when needed), answering allow or not in the "apple alert" (requestAccess).
I need to add a view where the user can be able to change these authorizations afterwards if he wants. I added switch buttons so the user can make the choice to keep it authorized or not. But I can't find how to set the authorizationStatus.
Is there a way to change the authorizationStatus manually, from .authorized to .denied or opposite ?
I tried to prompt the apple alert using 'AVCaptureDevice.requestAccess()' but it doesn't show if the user already authorized before.
Any suggestion welcome :)
By security and privacy reasons developers have only one way - use system dialog for requesting authorization for access to protected system services.
But you can suggest for your users a short way to system settings. This code launches the Settings app and displays your app’s custom settings.
let settingUrl = URL(string: UIApplication.openSettingsURLString)
DispatchQueue.main.async {
UIApplication.shared.open(settingUrl!, options: [:], completionHandler: nil)
}

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

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.

"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.