Level: Beginner
Completion Time: 30 minutes - 45 minutes
Technology: iOS SDK
This is the fourth and final part of the series on iOS Multitasking. Today we will be covering Background Location. Similarily to the previous iOS Multitasking tutorials, this will be simple and easy to implement. So lets 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.

Now go to the target settings and go to the Build Phases Tab. Then in the Link Binary with Libraries add the CoreLocation Framework 

. 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. 
Step 2: Setting up the User Interface Code
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>
Then 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 the CLLocationManagerDelegate to) add the following code.
CLLocationManager *locationManager;
IBOutlet UIButton *startTrackingButton;
IBOutlet UILabel *alertLabel;
Then under the lower closing brace add the following 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 <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface BackgroundTrackerViewController : UIViewController <CLLocationManagerDelegate> {
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 2: Settup the interface
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 files owner and select startTracking:. Then drag the referencing outlet to the files owner and select 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 checked. Then go to the connections inspector. Then drag the referencing outlet to the files owner and select alertLabel.
Step 3: Implement the Location Tracker
Now go and open the 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.