Examining the Different Application States

The iOS includes events that you can handle in your application delegate so that you can monitor your application's current state. The following Try It Out shows the various states that an application goes through.

TRY IT OUT: Handling Application Event States

image

  1. Using Xcode, create a Single View Application (iPhone) project and name it States. You will also use the project name as the Class Prefix and ensure that you have the Use Automatic Reference Counting option unchecked.
  2. Add the following bold code to the StatesAppDelegate.m file:
    #import “StatesAppDelegate.h”
    
    #import “StatesViewController.h”
    
    @implementation StatesAppDelegate
    
    @synthesize window = _window;
    @synthesize viewController = _viewController;
    
    - (BOOL)application:(UIApplication *)application
          didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
       NSLog(@“application:didFinishLaunchingWithOptions:”);
       self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]
         autorelease];
       // Override point for customization after application launch.
       self.viewController = [[[StatesViewController alloc]
         initWithNibName:@“StatesViewController” bundle:nil] autorelease];
       self.window.rootViewController = self.viewController;
       [self.window makeKeyAndVisible];
       return YES;
    }
    
    - (void)applicationWillResignActive:(UIApplication *)application
    {
        NSLog(@“applicationWillResignActive:”); ...

Get Beginning iOS 5 Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.