Reputation: 128
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
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!
Upvotes: 3
Views: 280
Reputation: 128
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!
Upvotes: 0
Reputation: 14128
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.
Upvotes: 1