ios chapter 3 lifecycle

Upload: seethalakshmi-sethurajan

Post on 14-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Ios Chapter 3 Lifecycle

    1/2

    Nimap Infotech, B-204, Pawapuri Apts, Love Lane, Mumbai 400010. Tel.:- +91 986 935 7889 Email:- [email protected] Web:- www.nimapinfotech.com

    iPhone App Life Cycle

    One will have notice after NSObject there is pair of triangle brackets in which we declare delegates protocols,

    which we will implement. Thus AppDelegate class needs to implement UIApplicationDelegate

    protocol, as it is this UIApplicationDelegate defines the iPhone application life cycle methods

    which iPhone OS sends the notification when ever application changes its state. Basically delegates are the

    components, which captures any events such as state change. Or in other words delegates are objects, which

    implements the protocol methods. Also it helps to implements a class various methods of the class, which is not

    the class hierarchy.

    Thus by declaring UIApplicationDelegate; AppDelegate class can implement the following methods (someare compulsory while some are optional)

    -(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

    -(void)applicationWillResignActive:(UIApplication *)application;

    -(void)applicationDidEnterBackground:( UIApplication *)application;

    -(void)applicationWillEnterForeground:( UIApplication *)application;

    -(void)applicationDidBecomeActive:(UIApplication *)application;

    -(void)applicationWillTerminate:(UIApplication *)application;

    These all above functions defines the life cycle of the iPhone Apps. One need to write its own implementation

    depending upon what app needs to do in the respective states.

    When OS prepares the application to run it sends control to didFinishLaunchingWithOptions.This method will

    call when user presses the app icon on the iPhone screen. In this function we define the window of our respective

    application on which content of the application will be displayed to user.

    In order to start write code in this function we first see how life cycle function is called.

    To understand the life cycle properly we will do simple exercise. We will put NSLog statement in each of these

    functions. NSLog function allows you get output on the console at particular point in the code. So you come toknow what part of code was executing when any associated NSLog message is printed.

    So after putting the NSLog function inside all the life cycle method we get following output on the console

    didFinishLaunchingWithOptions

    applicationDidBecomeActive

    Thus as we discussed before, first DidFinishLaunchingWithOptions is called which prepares the application for

    running in the system. Once OS prepares the application it puts the application in active state thus

    applicationDidBecomeActive is called. The application remains in this state unless it is interrupted like

  • 7/30/2019 Ios Chapter 3 Lifecycle

    2/2

    Nimap Infotech, B-204, Pawapuri Apts, Love Lane, Mumbai 400010. Tel.:- +91 986 935 7889 Email:- [email protected] Web:- www.nimapinfotech.com

    incomeing call/sms or user pressing the home button.

    If user press the home button on system running 4.0 or above than we get following console output for the projectwe have started.

    applicationWillResignActive

    applicationDidEnterBackground

    This output tells that OS has deactivated the application and send it to the background. But when user press home

    button on system running below 4.0 than following is output

    applicationWillTerminate

    This indicates that application is died. With OS 4.0 we can switch between the active applications, which is

    called as Multitasking. Thus on system with OS 4.0 and above application is not terminated rather it is push into

    background.

    When user double tap on the home button multitasking panel is visible & user clicks on the application icon say

    FirstProject we get following output

    applicationWillEnterForeground

    applicationDidBecomeActive

    This means OS brings application from background to foreground and after application to foreground it is taken

    to active state.

    Note: When you press icon of the application in multitasking panel till it shows up cross image so that one can

    close the application. Ideally applicationWillTerminate delegate function needs to be called. But it is not called.

    So it is always advisable to store the application required states when OS calls

    applicationDidEnteredBackground.