CLLocationManager best practices - ios

I'm using a CLLocationManager (_locationManager) to track user location within my tab application to enable tracking movement in the background. There is a record button, which when pressed, begins tracking and drawing a polyline of the user's movements. The [_locationManager startUpdatingLocation] is called in viewDidLoad of the MapViewController within my UITabBarController. Is it best practice to tell the _locationManager to stop updating the location when the user switches to another tab (If the application is not recording)? Or should I let it keep going? The reason I ask is because i use kCLLocationAccuracyBestForNavigation if enabled in the settings, and I know there are battery concerns if left to run.

There is one basic rule: if you need the data, then keep it on, else, turn it off.

Related

Updating geofences with low accuracy in background, update position on map at high accuracy, does not work as intended

I'm writing an app which use the location for two purposes:
1) It shows the user's location on a map (with navigation-like accuracy) in a location manager instance and delegate, contained in a viewcontroller
2) In the background, even when closed, it gets the nearest 20 points of interest (list stored in userdefaults) and activates a geofence for these, so the user is noticed when getting nearby. This is done by lowest accuracy possible (3km) to save power, and set up in another location manager instance, initiated when the app is first opened.
The problem is, that the CLLocationmanager object seems to be static so when I call it from two different places, it's the same instance that's returned, and the accuracy, intervals etc, seems to be the same, since both delegates are called simultaneously all the time.
Therefore, the geofences setup method is called all the time, when the user just move a little bit, which I want the map to reflect.
Yes, there's only one location manager. So reconfigure it when you go into the background and when you come back into the foreground. It may be convenient to switch its delegate to another object at those times as well (so that each delegate object can focus on one problem).
the CLLocationmanager object seems to be static
Yup! CLLocationmanager object is a Singleton so different objects cannot be created. To Solve your problem you need check you application status, which can be done using :
[[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground
Once you know the application state you can configure CLLocationmanager's object properties accordingly. Hope it helps!

Do not show location service icon in status bar but still use background updates

I'm looking for some solution to handle the problem:
If I use locationManager.requestAlwaysAuthorization() there're appears an location icon in status bar and never disappears, so, it is the problem - user can think that my app drain battery, but it's not.
So, when I use .requestWhenInUseAuthorization() I can't call didUpdateLocations function to update location even I add fetch background update, the #selector does not allow to call that method.
I want my app get location updates in background without always showing the location icon in the status bar. How can I update location by fetch?
So, when I use .requestWhenInUseAuthorization() I can't call didUpdateLocations function
Yes you can. If you go into the background when you are updating locations, you can continue to update locations. What you cannot do is start updating locations from the background.
I want my app get location updates in background
Use deferred location updates or location monitoring. This will keep your use of sensors to the minimum. Otherwise, the user's impression that you are using up the battery while in the background is entirely justified.

Region monitoring when device is off

I have a client requirement to monitor a region (say MyHome). To alert the user when he leaves MyHome and reaches back to the location. I can handle it pretty easily using the region crossing delegate methods:
locationManager:didEnterRegion:
locationManager:didExitRegion:
My question is, what will be the scenario if my device is off when I am at MyHome location. I leave the location MyHome, move to a different place and switch on my mobile. Will I get the locationManager:didExitRegion: delegate method fired when I launch the app back and thus will be able to notify that I am away from the region. OR will I have to do anything else to get this possible?
First of all monitoring region is not made for such small area to monitor. You may not get actual results. Now, the answer of your question, yes. It is possible. When you will start your device, you will have delegates method to get hit.

How to force ViewController to unload?

I have an application that runs on background because it's needs to use Location Update. however, since i don't want to consume battery. I would like to make sure that all view controller is unloaded when app switch to run on background.
Forcing your view controller to unload is not going to save any battery life. It's best to let the operating system worry about taking your app out of memory when it needs to. If you're using the background mode that notifies your application when the user has travelled a significant amount you need to worry about this even less since your app is not actually running in the background but only given a chance to respond to location changes. If you're using the constant location background mode (for GPS apps, etc.) this is a little bit more of an issue.
You don't need to unload the view controllers, but you do need to turn off location updates. If you are starting location updates in your App Delegate, put in something like this:
- (void)applicationWillResignActive:(UIApplication *)application
{
[myLocationManager stopUpdatingLocation];
}
You can also do this in the viewWillDisappear: method if your location manager is started in a different place than the App Delegate.

CLLocationManager don't stop

Dear fellow developers,
I am trying hard to find a solution for my problem regarfing CLLocationManager.
I use a CLLocationManager instance in my Application. If the user selects the Home button on the device or terminates the application I want the location services to stop.
Therefor I call [self.locationManager stopUpdatingLocation]; - But this somehow doesn't work. The application enters the background and the small location arrow in the upper right corner of the status bar don't disappear. Even if I add [self.locationManager release] or self.locationManager.delegate = nil; - the location tracking don't stop :-/
It only disappears if I go to my device settings and switch off location services for the app. Whenever I switch back to location service enabled I immediately get a purple colored arrow next to the switch toggle and the icon reappears in the status bar.
My question is how can I turn off location services when the app enters the background or is terminated?
Thanks a lot in advance and have a nice day :-)
Your location is disabled. The location service icon "meaning" has changed on iOS 5. Take a look at this question: https://apple.stackexchange.com/questions/27463/why-is-the-location-services-icon-always-present
I Quote the answer:
It's a new feature in ios 5 called "region Monitoring"
The reason it's active even if the app is closed is that this feature
runs in iOS 5 core and notifies all apps that are registered when they
have entered or left a specific geo-fence.
Reminders does that when you use a location based reminder.
Although the location icon appears at all time. This actually has very
minimal impact on the battery due to apple really optimizing this
feature by using cell and wifi mostly.
Your app is working ok. The system behaviour is the one who changed.
You do it the right way. When entering background, it's ok if some delegate methods are called for some seconds. That should stop.
Where do you stop the location updates ? Are you sure it is triggered ? If yes, are your delegate method called even if the visual indicators tell something else ?
Are you sure you don't trigger a method that reactivate the location update after you have stopped it (because for example you can receive some updates even after stopped).
For instance if you started monitoring a significant location change, then you should unsubscribe from it with the corresponding pair method. If you are using region enter, then until you unsubscribe, the system will notify your delegates.

Resources