Delay app until the user allows or disallows current location - ios

Is there a way to delay code from running until the user has answered the current location prompt? My app fetches annotations and puts them on the map. When the app launches for the first time and the user allows current location it fails to fetch any annotations because the code already ran without receiving a current location. Thanks in advance.

You can implement the following CLLocationManagerDelegate method:
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
and only show your annotations after that method is called. It will be called when the user accepts or rejects the current location prompt.

Related

In iOS is there a way to simulate a Geographical region crossing when app is installed while within location

In my app, I am using the Boundary-Crossing Events for a Geographical Region to determine if a user has visited a location. This is working as expected when users have installed the app prior to visiting the location, however some users will be installing the app while they are at the event, and therefore are already within the location when the event is installed and so iOS does not treat this as a boundary crossing.
Is there a way to force iOS to trigger the locationManager:didEnterRegion method if the startMonitoringForRegion method is called while the device is currently within the region to monitor?
In iOS7, you can use requestStateForRegion: method of the CLLocationManager class to check whether the user is already inside a fence.
You need to implement below delegate method to receive the event.
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
}
Thanks to this article for the answer (as well as a solution for iOS version < 7: http://hayageek.com/ios-geofencing-api/

iOS: Detect GPS service availability programmatically within an app

I want to attach a listener in IOS which is invoked whenever GPS is turned ON or OFF from settings. Listener should be invoked no matter my application is running, in background or has been stopped. I have this functionality that i need to keep GPS settings of a user who has installed app, on server side, so whenever GPS is changed i must notify the server.
I guess there isn't any listener,
You can Use Delegate method of CLLocationManager.
Use this delegate method for getting location - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
Form this [locations lastObject] you can latest location send location to server.
You can not get location when app is stopped.
As of now, there is no such notification exists.
However you can create and start a timer (NSTimer) that will regularly poll whether GPS is enabled or not, using 'CLLocationManager locationServicesEnabled' method.
Within your class, you can have a bool iVar that will be set / reset based on return value. Whenever its value is altered, you can notify server about service start / stoppage.

Observe location services updates

How can I observe and call a method when the location services options change? For example, my app runs location services in the background, i.e. the option in Settings is set to always. What if the user changes the option while my app is still running, how can observe changes and make changes in my app accordingly?
Implement the CLLocationManagerDelegate method
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
From the docs:
"This method is called whenever the application’s ability to use location services changes. Changes can occur because the user allowed or denied the use of location services for your application or for the system as a whole."

iOS mapKit refresh direction when the user's current location is updated

I am developing an application that allows the user to show the itinerary from the current location to the destination.
The itinerary must be refreshed when the user's current location changes.
I am using the LocationManager to detect the current location.
When the current location changes, i re-fetch the directions and display a new itinerary that starts from the new current location.
Is there a better and cleaner solution to achieve this ? Thanks.
What you are doing is correct way. Use following method of CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
}
to get location update and chage the display accordingly.

CCLocationManager popup

I have a locate me button in my app. The behaviour when user taps the button is quite strait forward. If location services is switched off I show an alert view to user about it otherwise starts updating location.
To check that locations services is enabled/disabled I start CLLocationManager and if I get the error code kCLErrorDenied in
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
I show the UIAlertView that the location service is disabled.
It works good but the problem happens when I start CLLocationManager for the first time (or user resets his locations services settings). In this situation system shows alert which asks user if he's ok that app will use location services and if user does not allow to use location services I immediately get error in
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
and should show second app's alert that locations services are disabled for the app but it's not really smart because user disabled it just second ago.
So the question is how to find out that system location services alert was on screen and user disabled location services second ago to not show second alert?

Resources