Continue process after user has allowed sharing and location found - ios

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.

Related

App get in LocationManager approval loop

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/

CLLocationManager last known location

To get the last know geolocation, I have:
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
CLLocation * lastKnownLocation = [locationManager location];
Does the lastKnownLocation reflect the last location my app has been called back with or does it reflect a global iOS system level last location (which other app or the iOS might have queried for)?
I am doing this instead of actively querying for user's location so I don't waste the user's battery.
If user has not given permission to access location, obviously lastKnownLocation should be nil. My question is specifically for the case where user has given my app permission to access location.
Generally you should not simply rely on the existing (if any) value returned from location unless you check its accuracy and time stamp (compared to your requirements). It's better to start startUpdatingLocation until you get a location which does match your accuracy requirements and then either stopUpdatingLocation or switch to stopMonitoringSignificantLocationChanges (if available).
The location returned from location is very dependent upon history, so you can't definitely say one way or the other that it will be. You always need to verify the accuracy for your purposes.
I know this is an old question but for the sake of being complete and maybe others ending up here:
The CLLocationManager class has the location property which returns (according to official Apple docs):
The most recently retrieved user location.
This method will return nil when no location is known.

iOS- How to avoid the dialog "Would like to use your current location"

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.

didUpdateToLocation alert called only twice

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.

WeeApp iOS Notification Center Widget: Location Services doesn't save permissions (jailbreak)

I'm making a Notification Center for jailbroken iPhones using the theos templates (so it runs on WeeApp). It's a weather widget, and I want it to be able to get the phone's current location so it can get weather from the closest station. I'm currently using the following code to start getting locations:
i_locationManager = [[CLLocationManager alloc] init];
i_locationManager.delegate = self;
i_locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
i_locationUpdated = NO;
[i_locationManager startUpdatingLocation];
and I have a didUpdateLocations method. All of that works fine. The problem is with the pop-up the phone uses to get permission to use the location. First of all, it says that SpringBoard wants to use the location. Is there any way to get it to say my widget's name instead?
More importantly, the saved permissions don't stick. They last as long as the phone is running, but every time I respring and open the Notification Center again, it re-asks for permission to use the location.
This isn't a fatal issue, of course, but it's irritating. Is there any way to get the phone to remember that the widget is allowed to use the current location?
This might help
[CLLocationManager setAuthorizationStatus:YES forBundleIdentifier:#"com.apple.springboard"];
This will authorize SpringBoard programmatically. First time you can display pop-up and save somewhere that user authorized you. Then you can do it yourself everytime you need location.
As for application name in pop-up. You can try hooking UIApplication, SBApplication, NSBundle methods that return application name. I don't think there is easier way to do it.

Resources