Sunday, September 30, 2012

Using location services in the background

 Cómo usar los servicios de localización en segundo plano

One of the most common problems when developing apps that require the use of location services in iOS is that we can not use them in the background constantly. kills iOS applications within 10 minutes of being in the background. This article will teach you to avoid this annoying problem.

Location services in the second plane

Libraries begin including location and our MainViewController.hy CLLocationManagerDelegate locationManager declaring to be accessible:

# Import <CoreLocation/CoreLocation.h>

@ Interface MainViewController: UIViewController <CLLocationManagerDelegate>
{
}

CLLocationManager * locationManager;

Once we have this, we will work MainViewController.my add this block at the very beginning, before @ implementation:

@ Interface AppDelegate: UIResponder <UIApplicationDelegate>
+ (Void) locationManager: (CLLocationManager *) manager didUpdateLocations: (NSArray *) locations;
@ End

In viewDidLoad will include the code that starts the GPS system of iOS 6 :

locationManager.distanceFilter = 100;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[LocationManager startMonitoringSignificantLocationChanges];
[LocationManager startUpdatingLocation];

In this case what we have done is put the GPS in social way to consume very little battery adjusting the filter 100 meters away and hundreds of meters accuracy. Activate also SignificantLocationChanges mode for the GPS is activated only when these conditions are met. You can omit these three lines and use only startUpdatingLocation for maximum accuracy, but the device's battery will die at a rate of 10% per hour.

DidUpdateLocations function is called each time there is a change of location.

- (Void) locationManager: (CLLocationManager *) manager didUpdateLocations: (NSArray *) locations
{
CLLocation * loc = [locations objectAtIndex: [locations count] - 1];

latitudeMe float = loc.coordinate.latitude;
longitudeMe float = loc.coordinate.longitude;
}

This location will operate, but not in the background. To achieve this objective in the AppDelegate will include these changes.

- (Void) applicationWillResignActive: (UIApplication *) application
{

NSLog (@ "to background");

UIApplication * app = [UIApplication sharedApplication];

NSAssert (== bgTask UIBackgroundTaskInvalid, nil);

bgTask = [app beginBackgroundTaskWithExpirationHandler: ^ {
dispatch_async (dispatch_get_main_queue (), ^ {

if (bgTask! = UIBackgroundTaskInvalid)
{
[App endBackgroundTask: bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
}];

/ / Start the long-running task and return immediately.
dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {

locationManager.distanceFilter = 100;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[LocationManager startMonitoringSignificantLocationChanges];
[LocationManager startUpdatingLocation];

NSLog (@ "App staus: applicationDidEnterBackground");
dispatch_async (dispatch_get_main_queue (), ^ {
if (bgTask! = UIBackgroundTaskInvalid) {
[App endBackgroundTask: bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
});

NSLog (@ "backgroundTimeRemaining:% .0 f", [[UIApplication sharedApplication] backgroundTimeRemaining]);

}

The block will dispatch_asynch that periodically reactivate location services. This way we can achieve the impossible and that is that our app alive in the background 24/7.

NOTE: This method does not work in Xcode 4.4 for iOS requires libraries compiled six but once work on any iOS 4, 5 or 6 and iPhone 3GS, 4, 4S, 5 and iPad 2, and 3.

Share this article with your friends on Facebook, Google and Twitter with the buttons you'll find at the beginning of it. Thank you!

iPadizate , best blog on the Apple iPad.



No comments:

Post a Comment