0

Considering the impact on battery, I would like to let users decide if the functionality of my app is something they want all the time as in "multitasking/background execution" or just when the app is in the foreground. Reading this thread:

How do I get a background location update every n minutes in my iOS application?

gave me some ideas about what might be the best way to provide a user option to enable the multitasking function. I am hoping someone who has considered this dilemma may have a suggestion on the "best way" to go about it.

Problem - Location multitasking is enabled via the plist by setting the UIBackgroundModes item to 'location'. This is app-wide and permanent. How to make the function correspond to a NSUserDefault?

Potential Solution A - My proposal goes something like this:

- (void)applicationWillResignActive:(UIApplication *)application {
    if ( [[NSUserDefaults standardUserDefaults] boolForKey:@"wantsBackgroundLocationEventProcessing"] ) {
        //Continue using the location subsystem
    } else {
        self.locationManager = nil; //Custom setter does all required cleanup
    }
}

However, my fear is that in this case the app will still receive location events, it simply won't be able to actually do anything with them - the effect on battery life will still be present.

Any ideas on how to make UIBackgroundModes 'location' optional during runtime?

Community
  • 1
  • 1
SG1
  • 2,871
  • 1
  • 29
  • 41

1 Answers1

1

the setting in plist just tell the iOS you need background update on location WHEN NEEDED. please check out the document here for better understanding.

the [locationManager startUpdatingLocation] will continuously update location change, when you does not need it any more, just use [locationManager stopUpdatingLocation] to turn off GPS to conserve battery.

or use [locationManager startMonitoringSignificantLocationChanges] to get notified when significant change happened.

Allen
  • 6,505
  • 16
  • 19
  • So you are basically accepting "Proposal A", which is that if I turn off location updates when entering the background then my app will not actively be registered and keep the location system draining the battery, regardless of the plist setting. I have been verifying that it works this way in practice, but am still not sure about whether the system is actually *off*, or just off for my app (assuming no other apps on the device). I do believe this answer is correct - can you confirm the latter point? – SG1 Feb 11 '12 at 05:17
  • the answer is yes, use [locationManager stopUpdatingLocation] first, then release your locationManager. – Allen Feb 11 '12 at 12:45
  • All my tests confirm you are absolutely correct. The docs you reference were ambiguous to me but knowing the right answer now I see they do state the truth. Thanks! – SG1 Feb 11 '12 at 21:39