iOS Lifecycle - kgleong/software-engineering GitHub Wiki

App Life Cycle

Main

Every iOS application has a main function that returns an instance of UIApplicationMain.

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[])
{
    @autoreleasepool {
        // Registers the `AppDelegate` class to receive life cycle callbacks.
        return UIApplicationMain(argc, argv, nil, String(AppDelegate);
    }
}

Life Cycle States

An application will always be in one of the following states:

State Description
Not running The app has not been launched, or was launched but terminated by the system.
Inactive The app is in the foreground, not receiving events, but may be executing code. Apps normally will enter this state as it transitions to or from the Active state.
Active The app is in the foreground, and able to receive events and execute code.
Background The app is in the background, and able to execute code. Apps will transition to this state before entering the Suspended state. Apps may request for more time in the Background state from the operating system if needed.
Suspended The app is in the background and not executing code. The app is still in memory, but unable to perform any instructions. The operating system will not send any notifications to the app when transitioning an app to this state. Any suspended apps are prime candidates to be purged from memory in favor of foreground apps if memory is low.

iOS app life cycle

References

App Delegate

The AppDelegate is where behavior can be customized at the application level.

There are numerous lifecycle callbacks that can be customized in the AppDelegate.swift file:

    /*
        Called when the application has been launched but is not visible to the user.
    */
    func application(
        application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?
        ) -> Bool {

        // Add any custom tasks here that should be performed once the
        // application has launched but before the app is displayed to the user.
        return true
    }

    // Called when an application is about to move to the foreground.
    func applicationDidBecomeActive(application: UIApplication) {
        /*
            Add any prepartion necessary for apps that may be transitioning from
            the background to foreground.  This might include:

            * restarting any tasks paused in `applicationWillResignActive`
            * refreshing the UI
        */
    }

    /*
        Called when an app is transitioning from background to foreground.

        More specifically, this method is called while the app transitions from the
        background to inactive state.

        The main difference between this callback and `applicationDidBecomeActive`:

        `applicationDidBecomeActive` may be called multiple times after an app has
        moved to the foreground, whereas `applicationWillEnterForeground` is only
        called once.

        E.g., If the user receives a phone call notification while using the app
        and dismisses it, only `applicationDidBecomeActive` will be called.
    */
    func applicationWillEnterForeground(application: UIApplication) {
        // Reverse any changes made upon entering the background state.
        // This is a good place to restore state.
    }

    /*
        This method will run when the application is interrupted.
        E.g., there is an incoming phone call/SMS message, or the user
        quits the application or switches to another application.
    */
    func applicationWillResignActive(application: UIApplication) {
        // For games, this is a good place to pause gameplay.
    }

    /*
        This method is called when the app has transitioned to the
        background.

        Code in this method is given approximately 5 seconds to complete.
        If this method does not return in the given timeframe, the app is
        terminated and removed from memory.

        If more time is needed, `beginBackgroundTaskWithExpirationHandler`
        can be used to request more time.
    */
    func applicationDidEnterBackground(application: UIApplication) {
        /*
            It's common to perform the following tasks here:
                * Any necessary UI adjustments/updates.
                * Release shared resources.
                * Aysnchronously save state so that the app state can be restored
                    when the app comes back to the foreground.
                * Invalidate timers.

            Code here should return as quickly as possible due to the time
            restraints that the system places on the run time of this method.

            Saving state should be done asynchronously on another thread.
        */
    }

    /*
        Called when an app is about to be purged from memory.

        This may be a result of the user terminating the app or the system killing
        the app to free up memory.

        Similar to `applicationDidEnterBackground`, this method must return within
        approximately 5 seconds.

        This method is NOT called if the app has already been suspended.
    */
    func applicationWillTerminate(application: UIApplication) {
        // Perform any necessary clean up or state persistence.
    }