Advertisement

iOS Multitasking: Background Location

by
This post is part of a series called iOS Multitasking.
iOS Multitasking: Background Tasks

This is the fourth and final part of the series on iOS Multitasking. Today we will be covering Background Location. Just like the previous iOS Multitasking tutorials in this series, this feature is simple and easy to implement. So let's start!

Step 1: Creating the Project

First, create a project with the View Based Application Template. Name the project BackgroundTracker and turn off Unit Tests.

Background Location

Now, go to the target settings and then go to the Build Phases Tab.

Background Location

In the "Link Binary with Libraries" section add the CoreLocation Framework.

Background Location

Now, under Supporting Files go to BackgroundTrack-Info.plist and add a new line with the key of Required Background Modes. It should become an array. Open the array and for Item 0 set the value for the key to App registers for location updates.

Background Location

Step 2: Coding the User Interface

Now go into the project sidebar and open up the BackgroundTrackerViewController.h. At the top, under #import <UIKit/UIKit.h>, add the following line:

#import <CoreLocation/CoreLocation.h>

To the immediate right of the UIViewController subclass declaration add the following line:

 <CLLocationManagerDelegate>

Then under the @interface declaration (right under the line you just added to the CLLocationManagerDelegate) add the following code:

 	CLLocationManager *locationManager;
	IBOutlet UIButton *startTrackingButton;
	IBOutlet UILabel  *alertLabel;

Then under the lower closing brace add these lines:

 	@property(nonatomic, retain) CLLocationManager *locationManager;
	@property(nonatomic, retain) IBOutlet UIButton *startTrackingButton;
	@property(nonatomic, retain) IBOutlet UILabel  *alertLabel;

	- (IBAction)startTracking:(id)sender;

Your .h file should like the following.

 	#import &lt;UIKit/UIKit.h&gt;
	#import &lt;CoreLocation/CoreLocation.h&gt;

	@interface BackgroundTrackerViewController : UIViewController &lt;CLLocationManagerDelegate&gt; {
	    CLLocationManager *locationManager;
	    IBOutlet UIButton *startTrackingButton;
	    IBOutlet UILabel  *alertLabel;
	}

	@property (nonatomic, retain) CLLocationManager *locationManager;
	@property (nonatomic, retain) IBOutlet UIButton *startTrackingButton;
	@property (nonatomic, retain) IBOutlet UILabel  *alertLabel;

	- (IBAction)startTracking:(id)sender;

	@end

Step 3: Setup the Interface XIB

Now open the BackgroundTrackerViewController.xib. First, drag out a UIButton to the middle of the interface so that the blue guidelines are in a cross shape. Then go the Connections Inspector. Drag the touchUpInside action to the file's owner and select startTracking:. Then drag the referencing outlet to the file's owner and select the startTrackingButton. Now drag a UILabel and position it above the UIButton. Set the width to be the the entire screen and set the text to say "Unable to find Location" (without quotes). Then in the Attributes Inspector set the Hidden checkbox to be checked. Then go to the connections inspector. Then drag the referencing outlet to the file's owner and select alertLabel.

Step 4: Setup the Location Tracker

Now go and open LocationTrackerViewController.m. Under the @implementation line add the following two lines of code:

 	@synthesize startTrackingButton;
	@synthesize locationManager;
	@synthesize alertLabel;

Now in the dealloc: method add the following lines of code under the [super dealloc]; line.

    [locationManager release];
    [startTrackingButton release];
    [alertLabel release];

Now add the following lines of code to the viewDidLoad: under the [super viewDidLoad]; line.

    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];
    //Only applies when in foreground otherwise it is very significant changes
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

And under the @synthesize declarations add the following method:

- (IBAction)startTracking:(id)sender {
    [locationManager startUpdatingLocation];
}

Then above the startTracking: method add the CLLocatioManager delegate methods (code below).

 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    CLLocationCoordinate2D currentCoordinates = newLocation.coordinate;

    [alertLabel setText:@"Location Has been found"];
    [alertLabel setHidden:NO];

    NSLog(@"Entered new Location with the coordinates Latitude: %f Longitude: %f", currentCoordinates.latitude, currentCoordinates.longitude);
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"Unable to start location manager. Error:%@", [error description]);

    [alertLabel setHidden:NO];
}

Wrap Up

Like the previous three, Background Location is extremely easy to implement. If you need any help or just have a helpful tip please comment below.

Advertisement