Important Lifecycles of IOS Development (Part1)

Today, I want to introduce two important lifecycles in IOS development: application lifecycle and the view controller lifecycle.

Regarding the lifecycle, even if you are only an iPhone/iPad user, it will be good for you to know what is happening behind the scenes. I will show you guys what happens before you launch an app, when you are in the app, and after you switch to another app.

The view controller lifecycle is more academic and it is the most basic and important thing that every iOS developer should be intimately familiar with if they don’t want to lose their jobs.

About the Application Lifecycle

Application Lifecycle BKLYN

                 Figure 1

From Figure 1, we can see all the states and the whole lifecycle of an application clearly. There are five states: 1) not running, 2) inactive, 3) active, 4) background and 5) suspended.

    1. Not running:  First of all, this is the state before you launch the app or after you kill the app.
    2.  Inactive: Running your code, no UI events. Notice any time when we enter into foreground, we first enter inactive state and then switch to active automatically.
    3. Active: When we are in the app doing all kinds of work, typically UI events, sending, receiving, processing. From the users view, when you are in the app doing all kinds of interactions/operations.
    4. Background: During this state, our code is still running, however the time will be limited. There are ways for developers to control the timing in this state and manage to do this. Notice that we can enter this state only from inactive.
    5. Suspended: After the app enters into the background, after a certain amount of time, it will become suspended. As a result, it is ready and can be killed at any time. Notice: no code runs between “suspended” and “not running”.

Operations

Launching the app: “not running” -> “inactive” -> “active”.

Function that we can override:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // Override point for customization after application launch.

        return true

    }

Switch to another app or click on home button or an incoming phone call or SMS message: “active” -> “inactive” -> “background” -> “suspended”.

Functions that we can override:

func applicationWillResignActive(application: UIApplication) {

        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions or when the user quits the application and it begins the transition to the background state.

        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

    }

func applicationDidEnterBackground(application: UIApplication) {

        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

        // If your application supports background execution, this method is called instead of applicationWillTerminate when the user quits.

    }

Switch back to an app in a short time: “background” -> “inactive” -> “active”.

Functions that we can override:

func applicationWillEnterForeground(application: UIApplication) {

        // Called as part of the transition from the background to the inactive state. Here you can undo many of the changes made on entering the background.

    }

    func applicationDidBecomeActive(application: UIApplication) {

        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    }

Leave a Reply

Your email address will not be published. Required fields are marked *