Suppose in the starting the location services are off in the default settings page. I have a button in the app to turn on the location services if first time I click on that it shows the default alert to change the settings to turn on
locationmanager = [[CLLocationManager alloc]init];
[locationmanager setDelegate:self];
[locationmanager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationmanager startUpdatingLocation];
It is working fine two times. but if it got third time location services are in off condition and click on on button it doesn't show any alert. I am unable to know the CLLocation behavior. May b its not a good question to ask but still I want to clear this concept. if anyone has some idea then please help me out.
Thank You.
Here's what Apple documentation says:
In addition to hardware not being available, the user has the option of denying an application’s access to location service data. During its initial uses by an application, the Core Location framework prompts the user to confirm that using the location service is acceptable. If the user denies the request, the CLLocationManager object reports an appropriate error to its delegate during future requests. You can also check the application’s explicit authorization status using the authorizationStatus method.
So the alert could or could not appear, based on authorizationStatus.
Related
I currently have my app setup to request location services always, using [locationManager requestAlwaysAuthorization]; and NSLocationAlwaysUsageDescription
This works fine, however I would like to give the option of using location services only while using the app like in the screenshot below.
I have tried adding NSLocationWhenInUseUsageDescription however this overides he always request and only gives the alert for while in use, any ideas on who to give both options in settings?
First ask the user using your own dialog with two options - always / when using the app. Then call appropriate permission request according to the user's choice.
Either:
[locationManager requestAlwaysAuthorization];
or:
[locationManager requestWhenInUseAuthorization];
having both in plist is valid so that's not a problem. Depends on when you actually request it in the code.
It will be slightly tricky to maintain though, so a good code structure is crucial.
The built in iOS alert will only allows for one level of permission and it will only ask the user once. The assumption is that most apps will only need one or the other. In any case, if you want to have both options show up in the Settings, you must ask for Always permission.
More importantly though:
The Always setting is really only to be used by apps that require background location updates. So unless your app requires it, you shouldn't be asking for it. Also, using background location mode will cause your app to be more heavily scrutinized during the app review process.
In my app, I have the following code;
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Start location services
if ([self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
locationManager = [[CLLocationManager alloc] init];
etc...
Somehow, when I do a fresh install, as soon as the location manager is started, the app gets in a loop, repeating the "Do you allow this app to use Location Services"-dialog faster than I can click OK or Cancel.
The way to get out of that loop is to switch to the Settings and manually
approve the usage.
This is iOS8, and I DID add the mandatory strings in the .plist.
What should I do?
Your problem is that you are requesting permission in applicationDidBecomeActive - When the permission dialog is shown your application becomes inactive (because there is a system dialog that is active) and then once the dialog is dismissed it becomes active again - but the permission has not yet been processed, so the dialog is shown and so on.
You should request location permission in another method - either applicationDidFinishLaunchingWithOptions: or in your view controller or other class where you want to instantiate your CLLocationManager - didBecomeActive is not a good place to do this.
You could init the CLLocationManager before requesting authorisation, but I would also recommend against just assigning for permission right away. The link here has a good write up on the most effective way to ask for permission: http://techcrunch.com/2014/04/04/the-right-way-to-ask-users-for-ios-permissions/
Is it possible to access the user's location on a widget?
I used the new iOS 8 API
[locationManager requestWhenInUseAuthorization];
and I added to the info.plist file the key
NSLocationWhenInUseDescription
however, when I request the location access, an alert should appear, but it doesn't happen.
Are there some not declared limitations on iOS today extensions?
As I found out, didUpdateLocations: delegate method doesn't work in today extension. So, to get location use locationManager.location.
Along with requesting authorization, you must also explicitly ask your location manager to start updating locations.
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
The key NSLocationWhenInUseDescription is wrong, therefore the Alert doesn't come up.
Change it to NSLocationWhenInUseUsageDescription and it should work.
For all keys you can refer to this:
https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html
The authorization dialog does not pop up in extensions. Your app, which should be in the same App Group (in capabilities)
In my app, one of my input is to get the location of the user as well as the contacts of the user. This is done from the code.
When the user runs the app for the first time,they get a dialog
"AppName" would like to use your current location. I wish to avoid this dialog since this is an important data and dont want the users to accidently press "Dont Allow"
How to avoid this dialog. Could any one please let me know. Thanks
You cant make use of location services or contacts without explicit permission from the user.
However, you can check for these permissions and tell the user that these services need to be allowed for them to use it properly.
Try looking at this answer for how to do that:
Checking for iOS Location Services
You cann't do this. If you try to do this apple will reject your app. Check this Doc1, doc2
Update Read this topic Location-Based Services
You can not dude, app will have to display the dialog
I think it's not allowed in ios.
the prompt is useful for use to know what permission of the app.
like map, photo and so on.
Basically if you are using the CLLocationManager to get the user's location, you can't. The user must allow your app to use location services. I think you could go around this by just dropping a pin on the map. For instance when you want the user to pick the location, you show the map and let the user tap where their location is, but that is not really user friendly :)
I will elaborate the process above, so that even if the user doesn't allow location services, you can get the users location manually. First you set up your CLLocationManager
_manager = [[CLLocationManager alloc] init];
_manager.delegate = self;
[_manager startUpdatingLocation];
and then you can observe it's delegate method
-(void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (kCLAuthorizationStatusAuthorized == status) {
//the app is authorized to use GPS
}
else {
//show map for manual location picking.
}
}
Hope this helps you to make some decisions.
I have something like this:
CLLocationManager *locManager = [[CLLocationManager alloc] init];
locManager.delegate = self;
locManager.desiredAccuracy = kCLLocationAccuracyBest;
[locManager startUpdatingLocation];
But I need to get below the latitude and longitude user. Obviously, trying to obtain locManager.coordinate just below the startUpdatingLocation my application crashes.
How can I make a condition to perform a process after the user allows sharing of location and it has been found?
Thanks in advance.
After calling startUpdatingLocation it can take some time until you get a location. The system first checks if location services are enabled for the app and asks the user to allow location services. The GPS hardware first needs to be turned on. It takes some time until it can fix the position.
So you should update the UI to show the user that there is something going on in the background. Consider using a UIActivityIndicatorView and maybe set userInteractionEnabled of the view to NO. Also it is a good practice to give the user the option to cancel the operation.
In your delegate you must implement these 2 methods:
– locationManager:didUpdateToLocation:fromLocation:
– locationManager:didFailWithError:
Here you can remove the UIActivityIndicatorView and reenable user interaction.
In case of success you can use the coordinate of the CLLocationManager to do whatever you want. In case of failure show an error alert.
Call stopUpdatingLocation when you don't need location services anymore to save battery.