3

I'm working on an application that uses location. The application can save a route on a map and if the user wants to record a route, the app have to work on background storing the user location to show it after that on the map.

To save the location on background I used the property NSLocationAlwaysUsageDescription defined on the plist of the application

NSLocationAlwaysUsageDescription on the app plist

My problem is that it's possible that user never wants to record any routes, but because of the way I'm defining that the app have to store location on background, my app seems to work always on background and it wastes so much battery even when could be not necessary to work on background.

I also start and stop the update of the location on my viewDidAppear and viewDidDisappear methods to avoid the update of the location when I'm not on the map screen:

    - (void)viewDidAppear:(BOOL)animated
{
    ...

    [[SBLocationManager sharedInstance] startUpdatingLocation];
    ...
}

- (void)viewDidDisappear:(BOOL)animated
{
    [[SBLocationManager sharedInstance] stopUpdatingLocation];
    [self.stationProvider stopProviding];
}

I've been looking about that but I don't find a way to set the background location on/off when I want. Do you know if there is a way to "enable/disable" the location on background while you're on the app to let the user configurate it?

Thanks so much for your help :) See you here!


Thais
  • 128
  • 7
  • Can you configure a distance change in location before your app notified about location update? – pronebird Dec 17 '14 at 15:01
  • If you stop updating the location when the view disappears, then how would you expect the location to update in the background? – Larry Borsato Dec 17 '14 at 15:08
  • @Andy you mean to change the distance when I'm letting the app on background? – Thais Dec 17 '14 at 15:22
  • @LarryBorsato well, when I'm on the map and user is recording his location I'm showing every second his new location and drawing it on the map, and I need to update it every time. But when the app is closed I store the location on background but I'm not asking every time for that. And when the app is opened again and the user open the map view again, I get the location stored while the app was on the background and I paint the route on the map – Thais Dec 17 '14 at 15:23
  • 1
    You have distanceFilter, desiredAccuracy, deferredLocationUpdates and a million other options, you better read documentation and pick up the most desired way: https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/clm/CLLocationManager/deferredLocationUpdatesAvailable – pronebird Dec 17 '14 at 15:28
  • @Andy ah yes sorry, I didn't understood you. I have the "best" accuracy defined because I want to track a bike route and I needed, like this: desiredAccuracy = kCLLocationAccuracyBest; distanceFilter = kDistanceFilter; headingFilter = kHeadingFilter; My problem is that it's possible to have some users that use the application but maybe they will never want to record a route, and I'm wasting many battery in a case that is no needed Do you know if there's a way to enable/disable the location on background in a case like that? Thank you so much for your help :) – Thais Dec 17 '14 at 15:42
  • @Andy and about the distance filter, maybe I understood wrong, but I don't change the distanceFilter or accuracy because I though that when I stop updating location this wouldn't be a problem with the battery, isn't it? – Thais Dec 17 '14 at 15:43
  • 1
    @Thais : Hope this helps: http://stackoverflow.com/questions/19042894/periodic-ios-background-location-updates – Mrunal Dec 17 '14 at 15:54
  • Thanks @Mrunal, it seems that could be a problem using stoplocation but not changing the accuracy. I'll try that solution and do some tests to see if that was the problem. Thanks for the help :) – Thais Dec 17 '14 at 16:00
  • Glad to know that. Please update us, if that works for you then. :) – Mrunal Dec 17 '14 at 16:02
  • 1
    Hi! I've been testing to add some changes that was on the question @Mrunal post yesterday and I found some changes that seem to improve a bit the problems with the battery, but I have to test it well to detect if is enough or not. I post the changes I did as an update on the first post – Thais Dec 18 '14 at 16:42
  • I guess that will solve your problem. – Mrunal Dec 18 '14 at 16:48
  • If you solved your problem add your question edition as an answer and mark as valid answer when stackoverflow allow you to do it. – rubdottocom Dec 24 '14 at 10:55
  • @rubdottocom ah yes, I think this will be more clear. Thanks! – Thais Dec 24 '14 at 10:56

2 Answers2

1

Adding this as an answer, so that if anyone else has the similar query they can refer.

Here is the similar question Periodic iOS background location updates

To prevent battery life, it requires to turn on - off location updates as per our reqiurements. Also there are few properties, desiredAccuracy and distanceFilter can be set in such a way so that it won't call location services frequently. (As mentioned in given link)

Hope this helps.

Community
  • 1
  • 1
Mrunal
  • 13,982
  • 6
  • 52
  • 96
0

I've been doing some changes according to the answers I got on the post that gave me a little improvement of the battery waste on my app, I have to test if is enough or if I can change something else, but here are the improvements I did, if it could be useful for someone:

I changed the startUpdatingLocation and stopUpdatingLocation on viewDidAppear and viewDidDisappear for that:

- (void)viewDidAppear:(BOOL)animated
{
    ...

    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [self.locationManager setDistanceFilter:kCLDistanceFilterNone];

    ...
}

- (void)viewDidDisappear:(BOOL)animated
{
    ...
        [self.locationManager setDesiredAccuracy:kCLLocationAccuracyThreeKilometers];
    [self.locationManager setDistanceFilter:99999];
    ...
}


I added two more properties to my locationManager to save battery:

_locationManager.pausesLocationUpdatesAutomatically = YES;
_locationManager.activityType = CLActivityTypeOtherNavigation;


And I found an error that was preventing location be stopped when you had close the application from the map view, because viewDidDisappear and stopUpdatingLocation wasn’t get called. Because of that, I added that on :

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self.locationManager stopUpdatingLocation];
    ...
}

I hope this will be useful for you too :) Thanks everyone for the help!

Thais
  • 128
  • 7