Tracing the iPhone Application Life Cycle

9/15/2010 11:45:37 AM

To help you get a sense for where your “work” in developing an iPhone application fits in, it helps to look at the iPhone application life cycle. Figure 1 shows Apple’s simplified diagram of the life cycle.

Figure 1. The life cycle of a typical iPhone application.

Let’s try to put some context around what you’re looking at, starting on the left side of the diagram. As you’ve learned, UIKit is a component of the Cocoa Touch that provides much of the foundation of iPhone applications—user interface management, event management, and overall application execution management. When you create an application, UIKit handles the setup of the application object via the main and UIApplicationMain functions—neither of which you should need to touch.

Once the application is started, an event loop begins. This loop receives the events such as screen touches, and then hands them off to your own methods. The loop continues until the application is asked to move to the background (usually through the user pushing the iPhone Home button).

Your code comes into play on the right side of the diagram. Xcode will automatically set up your iOS projects to include an application delegate class. This class can implement the methods application:didFinishLaunchingWithOptions and applicationDidEnterBackground (among others) so that your program can execute its own custom code when the application launches and when it is suspended by clicking the Home button.

Wait a second?! When does my application stop running!?

Beginning in iOS 4.0, applications no longer terminate when the user presses the Home button. Instead, the application stops where it’s at, and sits quietly in the background. When the user selects it from the task manager or starts it from the home screen, it returns to the foreground and continues exactly where it left off – automatically.

To support this process, the application delegate provides the method applicationDidEnterBackground, which is called when the application enters the background. This method should be used by your code to store any information that the application needs, in case it is terminated while it is the background – either by iOS cleaning up resources, or by the user manually terminating it in the task manager.

The previous method, applicationWillTerminate, can still be used if you develop an application that does not support backgrounding at all, but this is not the default.

After an application finishes launching, the delegate object typically creates a view controller object and view and adds them to the iPhone “window.” You’ll learn more about these concepts in the next hour, but for now, think of a view as what is being displayed on the iPhone screen, and the view controller as an object that can be programmed to respond when it receives an event notification (such as touching a button) from the event loop.

The majority of your work will take place within the view controller. You’ll receive events from the Cocoa Touch interface and react to them by writing Objective-C code that manipulates other objects within the view. Of course, things can get a bit more complex than a single view and a single view controller, but the same basic approach can be applied in most cases.

Now that you have a better picture of the iOS service layers and application life cycle, let’s take a look at some of the classes that you’ll be seeing throughout this book.

  •  Inside Cocoa Touch : Exploring the iOS Technology Layers
  •  The Language of Apple Platforms : Memory Management
  •  The Language of Apple Platforms : Objective-C Programming Basics
  •  The Language of Apple Platforms : Exploring the Objective-C File Structure
  •  The Language of Apple Platforms : Object-Oriented Programming and Objective-C
  •  Using the iPhone Simulator
  •  Introduction to Xcode Simulator
  •  Creating a Development Provisioning Profile on iPhone
  •  Preparing Your System and iPhone for Development
  •  Understanding Mobile Networking and Remote Access in Vista
  •  Creating Connections for Remote Access in Vista
  •  Configuring Mobile Connection Properties in Vista
  •  Establishing Mobile Connections in Vista
  •  Wireless Networking in Vista
  •  Configuring Power Management Settings in Vista
  •  Configuring Networking for Laptops
  •  Mobile Commerce Applications, Part 2
  •  Mobile Commerce Applications, Part 1
  •  J2ME: User Interface
  •  Java Me Programming Steps
    Top 10
    Creative software
    Getting the Most Out of the Microsoft Outlook Client : Using Cached Exchange Mode for Offline Functionality
    User Account Control Is More Than You Think
    Programming with DirectX : Game Input - Win32 Input
    Sharepoint 2007: Upload a File - Upload a File from the Web Interface
    Miscs Apps for iOS Device (April 2012) : SleepKeeper, Cristiano Ronaldo, NatureTap
    The New iPad: answer your questions
    SharePoint 2010 : Securing SharePoint’s SQL Server Installation
    Synchronizing Mobile Data - Using Merge Replication (part 2) - Programming for Merge Replication
    Programming Microsoft SQL Server 2005 : FOR XML Commands (part 3) - OPENXML Enhancements in SQL Server 2005
    Most View
    Wall Street’s “Technology Bubble” 2.0
    Securing Windows Server 2008 in the Branch Office
    Advanced ASP.NET : Component-Based Programming - Properties and State
    SharePoint 2010 : Outlining the Inherent Threat in SharePoint Web Traffic
    The ASP.NET AJAX Control Toolkit (part 3) - The AutoCompleteExtender
    Encrypt Your Entire Hard Drive with FileVault
    iPhone 3D Programming : Holodeck Sample (part 4) - Replacing Buttons with Orientation Sensors
    Infrastructure Security: The Network Level
    IIS 7.0 : Managing Configuration - Delegating Configuration (part 1)
    Performing a typical Exchange Server 2010 install
    Integrating Exchange 2010 with SharePoint 2010
    Angry Bird Space - They are back (Part 2)
    Mobile Payment: a future without a wallet
    Mobile SEO
    Understanding IIS 7.0 Architecture : Non-HTTP Request Processing
    The choices of mobile computing for SOHO users (part 2)
    Upgrading to Windows 8
    ASP.NET and AJAX
    Building Android Apps : Detecting Browsers with WURFL
    SQL Server 2008 Command-Line Utilities : The bcp Command-Line Utility