Application life cycle in iOS

Manasa M P
5 min readFeb 10, 2021

--

  • The application life cycle constitutes the sequence of events that occurs between the launch and termination of application.
  • It is very important to understand for all the iOS Developers, who wants smooth user experience.

Steps involved from device reboot to app launch:-

  • When user turn on the phone, no applications are running except app which belong to OS.When user tap on your app icon, SpringBoard launches your app.

SpringBoard is the standard application that manages the iPhone’s home screen. Other tasks include starting WindowServer, launching and bootstrapping applications and setting some of the device’s settings on startup

  • While springboard animates your app’s launch screen, your app and necessary shared libraries will be loaded into the memory. Eventually your app begins execution and application delegate receives the notifications.

AppDelegate is the application delegate object. It inherits the UIResponder class and implements the UIApplicationDelegate delegate protocol

  • Main entry into iOS apps is UIApplicationDelegate. It is a protocol and you need to implement that into your app to get notified about user events such as app launch, app goes into background or foreground, app is terminated, a push notification was opened etc.
  • UIResponder class make AppDelegate have the ability to respond to user events and UIApplicationDelegate enables the AppDelegate to be an application delegate object to manage and respond to the life cycle of the application.

Execution States for Apps:-

  1. Not Running state : The app has not been launched or terminated by the system.
  2. Inactive state : The app is entering the foreground state but not receiving events.
  3. Active state: The app enters the foreground state and can process event.
  4. Background state : In this state, if there is executable code, it will execute and if there is no executable code or the execution is complete, the application will be suspended immediately.
  5. Suspended state : The app is in the background(in memory) but is not executing code and if system does not have enough memory, it will terminate the app.

Mapping app states with Code:-

Flow of app life cycle from launch to suspended states:-

The Main Run Loop:

  • An app’s main run loop processes all user-related events.
  • App delegate sets up the main run loop at launch time and uses it to process events and handle updates to view-based interfaces.
  • main run loop executes on the app’s main thread
  • main thread is serial thread and this ensures that user-related events are processed serially in the order in which they were received.

Interview Questions on App life cycle:-

  1. How background iOS app gets resumed in foreground?

When user launches an app that is currently in background, the system moves app to the inactive state and then to the active state.

2. What are the steps involved when app enter to foreground after device rebooted?

When user launches an app for the first time or after device reboot or after system terminate the app, the system moves app to the active state.

3. What are the steps involved when app moves from foreground to background?

4. How can you opt out background execution?

  • You can explicitly opt out background execution by adding UIApplicationExitsOnSuspend key to application’s Info.plist file and setting its value to YES.
  • When you opt out background state, app life cycles will be between the not running, inactive, and active states and never enters the background or suspended states.

5. Which is the app state when device is rebooted?

Ans: Not Running state.

6. When app is running but not receiving event. In which state app is in?

Ans: Inactive state.

7. How an iOS app responds to interrupts like SMS, Incoming Call, Calendar, etc.?

Application moves to the inactive state temporarily and it remains in this state until the user decides whether to accept or ignore the interruption.

  • If the user ignores the interruption, the application is reactivated.
  • If the user accepts the interruption, the application moves into the suspended state.

8. What are the uses of background state?

  • It gives opportunity to save any application data that will help user to relaunch the app where they left.
  • App release any resources that don’t need.

9. How can you add additional background execution time for your app?

  • App can stay in background for few seconds and you can execute any code within that time.
  • You can call beginBackgroundTask(expirationHandler handler: (() -> Void)? = nil) method which requests additional background execution time for your app
  • To extend the execution time of an app extension, use the performExpiringActivity(withReason:using:) method

10. How can you check maximum amount of time available for app in background

backgroundTimeRemaining helps to get maximum amount of time remaining for the app to run in the background.

The value is valid only after the app enters the background and has started at least one task using beginBackgroundTask(expirationHandler:) in the foreground.

11. How can you debug your background task?

beginBackgroundTask(withName:expirationHandler:) for debugging background task.

I hope, above tutorial will help to clear the concepts of application lifecycle. Please hit the clap button below 👏 to help others find it!. follow me on Medium.

--

--