2

I have an App with Region Monitoring but for better presition I call to "startUpdatingLocation" for use the GPS when the user enter into the Region specified:

  - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
   [manager startUpdatingLocation]; 
}



-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
locationManager =manager;

     CLLocation *location1 = locationManager.location;
     CLLocation *location2 = [[CLLocation alloc] initWithLatitude:obj.latitude longitude:obj.longitude];
    float betweenDistance=[location1 distanceFromLocation:location2];


    if((betweenDistance/1000)<=0.350000){
            // Fires an UIAlert
     }

}

That works fine in the most of cases, for example if the user is outside the region and then press the "Home Button" and after enter into the region fires the function "didEnterRegion" and the GPS starts to work for a few minutes and show the Alert, thats the right way.

But if the App is open and then the user enter in the region fires the function "didEnterRegion" and the GPS starts, but if the user press the "Home Button" in that moment the GPS stops and the Alert never appear.

I don't want to activate the option of "Required Background modes" in the info.plist because I only want to use GPS for a few minutes after the user press de Home Button, and not for to use in a excesive mode.

Any ideas?

1 Answers1

2

When your application delegate gets the message applicationWillResignActive: you can request that the OS keeps your app running by creating a request like this:

UIBackgroundTaskIdentifier taskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
    // do you cleanup in here if your background task gets terminated by the system
}];

After that, your event loop will keep running and you'll continue to receive events like normal (except your app's window isn't visible, so you obviously won't receive touch events or other window-related events).

Once you no longer need to be kept running you can release your request like this

[[UIApplication sharedApplication] endBackgroundTask:taskID];

To make it all work, just add a flag to your app delegate to let it know when you're doing something that requires more time. Then, when your delegate gets the applicationWillResignActive: message it can check that flag and, if it's set, request some background time to keep running.

John Stephen
  • 7,625
  • 2
  • 31
  • 45
  • Thanks, but my CLLocationManager Delegate is in another view, and I need to call the method "startUpdatingLocation" of the same CLLocationManager until the user gets of your destiny. I don't know how to do it inside "applicationWillResignActive" in the AppDelegate. – Roberto Briones Argüelles Feb 04 '13 at 06:19
  • The background task stuff just tells the system to keep your app running. It doesn't matter where your CLLocationManager is, just communicate back to the app delegate that the app needs to be kept alive for a while longer and then the delegate can tell the system when the app is asked to resign. – John Stephen Feb 04 '13 at 21:57