Updating location in the background on iOS only occasionally

I am working on an application that uses location. The app can save the route on the map, and if the user wants to record the route, the app has to work with the background, saving the user's location to show it on the map after that.

To keep the location in the background, I used the NSLocationAlwaysUsageDescription property defined on the application plist

NSLocationAlwaysUsageDescription on the app plist

My problem is that the user never wants to record any routes, but because of the way I determine that the app should store the location in the background, my app seems to always be running in the background and it does so much consumes when there is no need to work in the background.

I also start and stop updating the location in my viewDidAppear and viewDidDisappear methods to avoid updating 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 have searched for this but cannot find a way to set the background location whenever I want. Do you know if there is a way to "enable / disable" location in the background during your application so the user can customize it?

Thanks a lot for your help :) See you here!


+3


source to share


2 answers


I did some changes as per the answers I got in this post that improved the battery waste in my app a bit, I have to check if enough or if I can change something, but here are the improvements I made if it is might be helpful for someone:

I changed startUpdatingLocation and stoppedUpdatingLocation on viewDidAppear and viewDidDisappear for this:

- (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 ran into a bug that prevented the location from being stopped when you closed the app from the map view because viewDidDisappear and stopUpdatingLocation were not called. Because of this, I added that on:

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

      

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

0


source


Adding this as an answer so that if anyone else has a similar request it can link.

Here is a similar question Periodic iOS background location updates



To prevent battery life, it is required that the updates are updated according to our requirements. There are also a few properties, it is desirable that the "Precision" and "DistanceFilter" functions can be set so that they do not often call location services. (As mentioned in this link)

Hope it helps.

+1


source







All Articles