iOS Multitasking: Background Location

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:

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

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

Then under the lower closing brace add these lines:

Your .h file should like the following.

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:

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

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

And under the @synthesize declarations add the following method:

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

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.

Tags:

Comments

Related Articles