iOS Multitasking: Local Notifications

Since the release of iOS 4, many amazing features like folders, single inbox, and more have been available to iOS developers. One of the most anticipated features for iOS is multitasking. Multitasking behaves very differently on iOS than on other mobile operating systems, and this post will guide you through how to set it up!

There are six major components of the multitasking feature. They are:

  • Notifications
  • Task Completion
  • Background Location
  • Background Audio
  • Fast-App Switching
  • Voice Over IP

In this four part series we will be covering these elements of multitasking with the exception of Fast-App Switching and Voice Over-IP. In this tutorial, part 1 of the series, we will be covering notifications, specifically local notification, because push notifications require external server configuration. With Local Notifications you will be able to build timers, alarms, and many other great applications. In this tutorial we will be creating a cooking timer to prevent over-cooking your food.

Step 1: Create the project

To start off building this project you will open Xcode. Next select New View Based Application. Name the project what you wish, I will be calling mine "Cooker". Once you've named your application enter the company identifier and set the device to iPhone. Be sure to check "Include Unit Tests" if you are inclined to. Then choose where to save the project and create the project.

Create a Receiver Project

Step 2: Setting up the interface code

In the "Cooker" directory in Xcode select CookerViewController.h to start building the class interface. Under the @interface declaration between the brackets add the following lines of code:

Now under the closing bracket add the following code:

Your CookerViewController.h file should look like.

Step 3: Build the interface

In the "Cooker" directory in Xcode find and open CookerViewController.xib to start building the user interface. First drag out a UIDatePicker to the top of the view touching the status bar. In the Attributes Inspector select, in date picker mode, "Count Down Timer".

Create a Receiver Project

Go to the Connections Inspector and drag a referencing outlet to the files owner and connect it to the datePicker property.

Next get a UITextField and drag it to the middle so it is touching the bottom of the UIDatePicker.

Go to the Attributes Inspector and set the Placeholder to "Enter your message here." and set the return key to "Done".

Create a Receiver Project

Now go to the Size Inspector and set the width to 280 pixels.

Create a Receiver Project

Go to the Connections Inspector and drag the referencing outlet to the files owner and connect it to the alertBodyField property. Once you've done that drag the didEndOnExit action onto the files owner and select dismissKeyboard:.

Now below the UITextField drag out a UILabel and set the UILabel's text to "Notification Registered" or what you want the alertNotificationLabel to say and set the label to be hidden in the Attributes Inspector.

Go to the Size Inspector and set the width to 280 pixels, the x position to 20, and the y position to 293. Then go to the Connections Inspector and drag a reference outlet to the files owner property alertNotification.

Once you've done that drag a UIButton below the UILabel and set the text inside the button to "Start Timer".

In the Size Inspector set the x position to 108 and the y position to 403. Now go to the Connections Inspector and drag a connection from the touchUpInside action to the addNotification method in the files owner. Here is the finished interface.

Create a Receiver Project

Step 4: Implement the Local Notifications

Go to CookerAppDelegate.m and add the following line to application:(UIApplication *)application didFinishLaunchingWithOptions:.

Now in applicationDidEnterBackground and applicationWillTerminate: add the following line

Your app delegate should look like this

Go to the CookerViewController.m and add the following code to dealloc

Now add the following code below @implementation CookerViewController

Next add the following code above dismissKeyboard: and below @implementation CookerViewController

Here is the completed CookerViewController.m

Wrap-Up

If you build and run you should be able to make the timing perfect for all your cooking needs. Thanks for reading this tutorial, be on the look out for part 2 in the series, Background Audio. If you need any more help please comment.

Tags:

Comments

Related Articles