iOS Application States

App states:

  1. applicationWillResignActive

  // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) 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.

2. applicationDidEnterBackground

        // 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.

    3. applicationWillEnterForeground

        // 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.

    4. applicationDidBecomeActive

        // 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.

  5. applicationWillTerminate

        //iOS might terminate your app if system resources are low or user kill your app, then this method will get called. Save data if appropriate.

// applicationWillTerminate will only get called when your app is in background or foreground i.e This method is not called if your app is suspended while being terminated.

Here is the table of various states enter image description here

Q0. When an App is said to be in NOT Running state?
A. An app is said to be in ‘not running’ state when:
– it is not launched.
– it gets terminated by the system during running.

Q1. How to handle Push Notifications when App is terminated?

Ans:

case 1) When application is running in foreground or When application is running in background application:didReceiveRemoteNotification: method will called as below.

- (void)application:(UIApplication *)application didReceiveRemote
Notification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateInactive)
     {
     //opened from a push notification when the app was on background

       NSLog(@"userInfo->%@",[userInfo objectForKey:@"aps"]);
     }
  else if(application.applicationState == UIApplicationStateActive)
     {
     // a push notification when the app is running. So that you can 
display an alert and push in any view

        NSLog(@"userInfo->%@",[userInfo objectForKey:@"aps"]);
     }
}

case 2) When application is not launched (close i.e when the App is in not running state) then application:didFinishedLaunchWithOptionsmethod will called.

The app does not process push notification in the background, what it really does the OS is wake up the app once you press in the notification. You can catch this moment in the following way:

 - (BOOL)application:(UIApplication *)application didFinish
LaunchingWithOptions:(NSDictionary *)launchOptions
{
  if (launchOptions != nil)
    {
         //opened from a push notification when the app is closed
        NSDictionary* userInfo = [launchOptions objectForKey:UIApplication
LaunchOptionsRemoteNotificationKey];
        if (userInfo != nil)
        {
             NSLog(@"userInfo->%@",[userInfo objectForKey:@"aps"]);
        }

    }
     else{
             //opened app without a push notification.
         }
}

**Imp Ques :- What if, if you receive many notifications & you did not open the app, neither did you tap the system’s notification panel. How are you preserving those push for a later retrieval?

Ans: As per your question, there is no way to hold all the notification when you open the app, better you call an api to get all notification as per time stamp from your back end/server that’s how Facebook does.

Q2. Assume that your app is running in the foreground but is currently not receiving events. In which sate it would be in?

A. An app will be in InActive state if it is running in the foreground but is currently not receiving events. An app stays in InActive state only briefly as it transitions to a different state.

Q3. Give example scenarios when an application goes into InActive state?

A. An app can get into InActive state when the user locks the screen or the system prompts the user to respond to some event e.g. SMS message, incoming call etc.

Q4. When an app is said to be in active state?

A. An app is said to be in active state when it is running in foreground and is receiving events.

Q5. Name the app sate which it reaches briefly on its way to being suspended.

A. An app enters background state briefly on its way to being suspended.

Q6. Assume that an app is not in foreground but is still executing code. In which state will it be in?

A. Background state.

Q7. An app is loaded into memory but is not executing any code. In which state will it be in?

A. An app is said to be in suspended state when it is still in memory but is not executing any code.

Q8. Assume that system is running low on memory. What can system do for suspended apps?

A. In case system is running low on memory, the system may purge(remove completely) suspended apps without notice.

Q9. How can you respond to state transitions on your app?
A. On state transitions can be responded to state changes in an appropriate way by calling corresponding methods on app’s delegate object.

For example:
applicationDidBecomeActive method can be used to prepare to run as the foreground app.
applicationDidEnterBackground method can be used to execute some code when app is running in the background and may be suspended at any time.
applicationWillEnterForeground method can be used to execute some code when your app is moving out of the background
applicationWillTerminate method is called when your app is being terminated.

Q10. List down app’s state transitions when it gets launched.
A. Before the launch of an app, it is said to be in not running state.
When an app is launched, it moves to the active or background state, after transitioning briefly through the inactive state.

Q11. Who calls the main function of you app during the app launch cycle?
A. During app launching, the system creates a main thread for the app and calls the app’s main function on that main thread. The Xcode project’s default main function hands over control to the UIKit framework, which takes care of initializing the app before it is run.

More Explanation:

Not running :   The app has not been launched or was running but was terminated by the system.

Inactive : The app is running in the foreground but is currently not receiving events. (It may be executing other code though.) An app usually stays in this state only briefly as it transitions to a different state.

Active : The app is running in the foreground and is receiving events. This is the normal mode for foreground apps.

Background : The app is in the background and executing code. Most apps enter this state briefly on their way to being suspended. However, an app that requests extra execution time may remain in this state for a period of time. In addition, an app being launched directly into the background enters this state instead of the inactive state. For information about how to execute code while in the background, see Background Execution.

Suspended : The app is in the background but is not executing code. The system moves apps to this state automatically and does not notify them before doing so. While suspended, an app remains in memory but does not execute any code. When a low-memory condition occurs, the system may purge suspended apps without notice to make more space for the foreground app.

Most state transitions are accompanied by a corresponding call to the methods of your app delegate object. These methods are your chance to respond to state changes in an appropriate way. These methods are listed below, along with a summary of how you might use them.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s