iPhone™ Flashlight App: Hands-On for Software Developers

Norman McEntire

Copyright © 2009 Servin Corporation. http://servin.com

Revision 1.1 – 2009 January 7

Introduction

Since 1995, Servin Corporation has provided innovative and unique software technology services to Fortune 1000 companies. Servin is independent of any given technology vendor, hence providing a fair and unbiased view of the many software technologies.

Servin's software technology services focus on two major areas:

What follows is a mini-course on how to write an iPhone Flashlight App. In many ways, the iPhone Flashlight app is the “hello world” of iPhone software development. That is, once you've developed the iPhone Flashlight App, you have a starting point of knowledge for developing other iPhone applications.

If you like the flow of this mini-course, you or your company may be interested in the full version of the course, which can be held on-site at your company's location. As with this mini-course, our “iPhone for Software Developers” course is full of practical, hand-on, step-by-step training. We do not show slides during our courses, but rather show everything live, in action, step-by-step, just like the flow of this mini-course. Our hands-on training courses are the absolute fastest and best way to learn and master iPhone programming.

OK, let's get started with our mini-course.

Setup of iPhone Software Development Environment

If you have not already done so, follow these steps to setup your iPhone Software Development Environment. As a point of reference, I performed all of these steps in a couple of hours, and that included purchasing the MacBook, and downloading the iPhone SDK.

  1. Get an Intel-based Mac. In my case, I went to Fry's and purchased a MacBook, which was selling for $1295 when I purchased it, and included a 2 Ghz Intel Core Due, 2 GB of RAM, and a 160GB disk. Note that an older Mac that used the PowerPC processor will not work for the iPhone SDK; it must be an Intel-based Mac.

  2. Join Apple Developer Connection, or ADC. Simply point your browser to http://developer.apple.com and register as an Apple developer. Registration is free, quick, and easy.

  3. Download the iPhone SDK. Once you have registered on the ADC, you can download the iPhone SDK, which is also free. The download is over 1GB, so it may take a while depending on your network speed.

  4. To repeat: both registration on ADC and also download of the iPhone SDK is free. You only have to pay money if you decide to publish your app on the iPhone App store. You can do everything in this mini-course by using the iPhone Simulator, which is included with the iPhone SDK.

  5. Install the iPhone SDK. Once you have downloaded the iPhone SDK, double-click to install, answering all the questions as required. At the end of the installation, you are ready to develop iPhone applications.

Startup Xcode

Apple provides a tool named Xcode as the Apple Integrated Development Environment, or IDE, for both Mac OS X and iPhone development. Here you will startup Xcode.

  1. Click on Finder, which is at the bottom left of the screen on the Dock. This opens Finder, showing your disk and files on the disk.

  2. Near the top left of the Finder window, find the word Devices.

  3. Under the word Devices, observe the word Macintosh HD.

  4. Click on the Macintosh HD, and observe the Folder named Developer.

  5. Double-click on the folder named Developer.

  6. Look for the folder named Applications.

  7. Double-click on the folder named Applications.

  8. Look for the icon labeled Xcode.

  9. Double-click on the icon named Xcode.

  10. After a moment you should observe Xcode running, as shown by the top-level menu saying Xcode.

At this point Xcode is running, so the next step is to create your new project.

Create New iPhone Flashlight Project

In this step, you will create a new Xcode project named Flashlight.

  1. From the Xcode menu, select File > New Project.

  1. Click on iPhone OS Application category (if not already selected).

  2. Click on Window-Based Application.

  3. Click on Choose button.

  4. Enter the name Flashlight.

  5. Click on Save button.

The result of the above steps is that Xcode will create a new project named Flashlight that includes template files to start your development.

Build and Run Default Flashlight Project

Here you will build and run Flashlight, observing the results of the default build.

  1. In Xcode, find the icon in the top tool bar labeled Build and Go.

  2. Click on the Build and Go, which will build your project, and start it, running it in the iPhone Simulator.

  3. NOTE: If the iPhone Simulator is already running, you may get an error in Xcode saying it cannot startup the iPhone Simulator. If you get this error, press “Command+Tab” key until you select the iPhone Simulator ,and then exit the simulator.

  4. Observe the Flashlight App running in the iPhone Simulator, making note of the top-level Status Bar, and the all-white window.

  5. Press the Home key on the iPhone Simulator (at the bottom of the iPhone) to stop the Flashlight app and return to Home on the iPhone Simulator.

Enhancing the iPhone Flashlight Application

While the default Flashlight is a good start, we need to enhance the application as follows:

  1. Hide the Status Bar.

  2. Make sure background of window is always white.

  3. Disable timer so that display does not turn off after a short delay of non-use.

Hide Status Bar

Since we want as much light as possible from the Flashlight, it makes sense to hide the Status Bar that is at the top of the Window. Follow these steps to create the code to hide the Status Bar, followed by a rebuild and a re-run in the iPhone Simulator:

  1. Press the Command+Tab keys until you return to Xcode.

  2. Looking at the Xcode, on the left you will see a window named Groups & Files.

  3. Under the Groups & Files, you will see Flashlight.

  4. Under Flashlight, you will see a folder named Classes.

  5. Click on the Classes folder.

  6. After clicking on the Classes folder, you should see two files listed under File Name on the right side of the screen:

  1. Click on FlashlightAppDelegate.m.

  2. Observe the FlashlightAppDelegate.m code shown on the lower right portion of the Xcode screen, looking for a line of code that looks like this:

  1. This method, applicationDidFinishLaunching:, is called at the end of your application launch.

  2. At the end of the line, notice the application parameter. This parameter gives you access to the application object that is the heart of your iPhone application.

  3. Observe the line of code that looks like this:

  1. This is a comment, and it tells us to add code below this point to customize the Flashlight application after launch. This is indeed what we want to do, i.e., we want to hide the Status Bar after our application launches.

  2. At the line below the comment, start typing the following code, but instead of pressing the return key at the end of the line, press the ESC key to display the list of choices at this point in your code:

  1. To repeat: you should type [application, and then press the ESC key. This will show you all the choices you can enter next in your code.

  2. With the list of choices displayed, use your mouse to scroll down until you find the choice named setStatusBarHidden: (observe that the : is part of the name).

  3. Double-click on setStatusBarHidden:, and you will see the following line of code in your FlashlightAddDelegate.m:

  1. Observe that “(BOOL)” is highlighted, and if you immediately type YES, the (BOOL) will be replaced by the YES, resulting in your line of code looking like this:

  1. Complete the line of code by adding the closing ] and a semicolon:

  1. Make a mental note of what the above line says: “Send the message named setStatusBarHidden:, with the argument YES, to the object referenced by application. This line of code is Objective-C, which is a super-set of the C programming language.

  2. Again, make sure you know how to “say” the line of code above: “Send the message names setStatusBarHideen: with the argument YES to the object reference by application.

  3. Once again click on Build and Go.

  4. Assuming you did not have any errors from the previous step, you should see the Flashlight App running in the iPhone Simulator with the Status Bar hidden.

  5. While in the iPhone Simulator, press the Home button to stop the Flashlight application, returning to the home screen.

As a review, in this step you used the Objective-C message passing syntax to send a message to the application object, telling it to hide the Status Bar.

Set Window Background To White

Although the window background is set to white by default by the template, in this step you will set the window to a white background under program control. Along the way, you will temporarily set the window background to red, to confirm that you indeed have control of the window background.

  1. Press Command+Tab to return to Xcode.

  2. The FlashlightAppDelegate.m code should still be visible from the previous exercise. If not, click on the FlashlightAppDelegate.m name to make the code visible in the editor.

  3. Under the line of code you added in the previous exercise, start typing the following code, including the “.” at the end of the word window, then press the ESC key:

  1. After pressing the ESC key, you should again observe a pop-up window, showing you choices.

  2. Using your mouse, scroll to find backgroundColor.

  3. Double-click on backgroundColor, which results in your code looking as follows:

  1. Continue typing the line so that it appears as follows, once again pressing the ESC key at the end of the text:

  1. After you press the ESC key, you will see the window of choices, and use your mouse to select redColor:

  1. Then go ahead and add the ending ] and ; to complete the line:

  1. The line of code you just entered says to do two things:

    1. Send a message named redColor to the class named UIColor.

    2. Set the window property named backgroundColor to the results of the [UIColor redColor] message.

  2. Click on Build and Go.

  3. Assuming there are no errors from the previous step, you should see that your window now has the background color of red.

  4. Click on the iPhone Simulator Home key to exit Flashlight.

  5. Return to Xcode, and change the redColor to whiteColor:

  1. Click on Build and Go.

  2. Assuming there are no errors from the previous step, you should see that your window now has the background color of white.

  3. Click on the iPhone Simulator Home key to exit Flashlight.

As a review, in this step you learn how to set the background property of the window object. You also learned how to send a color message to the UIColor class.

Disable Idle Timer

If you download your Flashlight App to a real iPhone or iPod Touch, and run it, the idle timer will dim the screen after a period of non use, and then turn the power to the screen off. When using Flashlight, you may want the “light” to say on until you manually turn it off. So, in this step, you will disable the idle timer when Flashlight starts, and then re-enable the idle timer when your application ends.

  1. Return to Xcode, to the same FlashlightDelegateApp.m file that you have been editing.

  2. Position to the next line that immediately follows the last line you entered from the previous step.

  3. Start typing the following line of code, being sure to press the ESC key at the end:

  1. When you see the window of choices, scroll until you find the setIdleTimerDisabled: message.

  2. Double-click on the setIdleTimerDisabled: message, resulting in your code looking as follows:

  1. Complete the line by entering YES, followed by the ] and ; characters:

  1. The above code sends a message setIdleTimerDisabled: with an argument of YES to the object reference by application. This will disable the idle timer while Flashlight runs.

  2. However, you also want to re-enable the idle timer when your Flashlight app stops running.

  3. To re-enable the idle timer when Flashlight ends, enter the following code between the applicationDidFinishLaunching: and dealloc methods:

  1. The applicationWillTerminate: message is received when your application is terminating, so this is an excellent time to disable the timer. You disable the timer by sending the setIdleTimerDisabled: message, with an argument of NO, to the object referenced by application.

  2. After entering the code from the previous steps, click on Build and Go.

  3. When you run this code in the iPhone Simulator, it will operate as before. That is, the simulator does not make use of the idle timer. However, if you run this code on an actual iPhone or iPod Touch, then your Flashlight will stay on until you exit your Flashlight application.

As a review, in this step you disabled the idle timer by sending a message to the application object at program startup. You also sent another message to the application object to re-enable the idle timer at program termination.

Using Xcode Research Assistant

In the previous step, how did I know about the applicationWillTerminate: message? The answer is that I used the Xcode Research Assistant, and in this step you too will find how to make use of the Xcode Research Assistant.

  1. Return to Xcode.

  2. The FlashlightDelegate.m code should still be visible in the editor window.

  3. Above the editor window is another window with the words File Name in the window.

  4. In the File Name, select the FlashlightDelegate.h file. (Again, that is the .h file, not the .m file that you selected earlier.)

  5. In the FlashlightDelegate.h file, find the word UIApplicationDelegate. It will be on a line like this:

  1. Double-click on UIApplicationDelegate to select the complete word.

  2. Using the Xcode menu, click on Help > Show Research Assistant.

  3. The Research Assistant window should open, displaying a link to the UIApplicationDelegate Protocol Reference.

  4. Click on the UIApplicationDelegate Protocol Reference, observing that an additional window opens, showing the reference manual for UIApplicationDelegate Protocol Reference.

  5. Look at the UIApplicationDelegate Protocol Reference window, observing the following categories of topics

  1. Under the category titled Tasks, observe that one of the tasks is:

  1. Double-click on the Controlling Application Behavior, and observe this choice:

  1. Click on the applicationWillTerminate: selection, observing the information about the message.

  2. In Xcode, use the menu option Window > Flashlight.xcodeproj to return to the main Xcode project window.

As a review, in this step you used the Xcode Research Assistant to help you find information related to writing iPhone code.

Using Xcode Console and printf()

Although Flashlight is a graphical app for the iPhone, you can send status and/or debugging information to the Xcode console by using the printf() function. In this step, you will put printf() functions in the FlashlightDelegate.m code, so you can see when the application starts, and when it ends.

  1. Return to Xcode, once again selecting the FlashlightDelegate.m file in the editor.

  2. Find the line of code that has the applicationDidFinishLaunching: method:

  1. Add the following line as the first line of code inside the method from the previous step:

  1. NOTE: The __FUNCTION__ is has two underscores before and after the word FUNCTION.

  2. Find the line of code that has the applicationWillTerminate: method:


  1. Add the following line as the first line of code inside the method from the previous step:

  1. Using the Xcode menu, select Run > Console.

  2. Observe that a window named Flashlight Debugger Console opens.

  3. In the Flashlight Debugger Console window, click on Build and Go.

  4. Assuming you did not have any errors from the previous step, you will see Flashlight run in the iPhone Simulator.

  5. Observe the following output in the console window:

  1. On the iPhone Simulator, press the Home button to terminate the Flashlight app.

  2. Obseve the output on the console:


As a review, in this step you learned how to use the Xcode console to view output from the print() function.


Review of Key Skills

As a review, you have learned the following skills during this mini-course on the iPhone Flashlight App:

Where To Go From Here

If you found this mini-course useful, you may want the author to give a live, 100% hands-on version of the complete course at your business. To arrange this, simply send a request to Servin Corporation.

If you would like other topics covered, simply watch this site, for other topics related to iPhone programming will be posted over the next several weeks.

In addition, if you have questions about this mini-course, or if you find errors or suggestions for improvement, feel free to send an email directly to the author at Servin Corporation.

Enjoy your iPhone Flashlight App!