iOS Architecture & Application Life Cycle

Rahul Singh
6 min readJun 21, 2018

--

Just as established products and brands need updating to stay alive and vibrant, you periodically need to refresh or reinvent yourself. By: Mireille Guiliano

The term Architecture means the ‘skeleton’ of the system. It can be visialized as the highest level of abstraction for that particular system.

iOS is a layered architecture comprising of 4 different layers namely Cocoa Touch, Media Layer, Core Service, Core OS (Image 1). Each of these layers is built with different frameworks that can be used and incorporated into the applications. Let us go through each of these layers in details.

Image 1

Cocoa Touch Layer

It is the topmost layer in the iOS architecture. It contains some of the key frameworks which native iOS applications rely upon. UIKit framework being the most prominent one. MapKit, PushKit, EventKit are some of the other vital frameworks.

It defines the basic application management and infrastructure, multitasking support, user interface management, multi touch support, motion event etc.

Media Layer

This layer provides multimedia services that you can use within your iPhone, and other iOS devices. The Media layer comprises of Graphics, Audio, and Video.

This layer consists of Assets Library, Core Text, Core Graphics, OpenGL ES and OpenAL, AV Foundation and Core Media. This layer is responsible for any kinds of drawing, rendering 2D and 3D data, buffereing video, Text layout etc.

Core Service Layer

This layer provides features such as block objects, Grand Central Dispatch, In-App Purchase, and iCloud Storage. Automatic Reference Counting (ARC) was introduced in this particular layer which takes care of all the Memory Management.

The Foundation framework is yet another essential framework for iOS and OS X applications.

Core Services layer is also closely tied to the C-based Core Foundation framework or Core Foundation. This has a feature referred to as toll-free bridging, which enables the substitution of Cocoa objects for Core Foundation objects in function parameters and vice versa.

Core OS

The Core OS layer holds the low level features that most other technologies are built upon. It also interacts directly with the hardware.

Most of the functionality provided by the three higher level layers is built upon the Core OS layer and its low level features. The Core OS layer provides a handful of frameworks that your application can use directly, such as the Accelerate and the Security frameworks.

The Core OS layer also encapsulates the kernel environment and low level UNIX interfaces to which the application has no access for so obvious security reasons. However, through the libSystem library, which is C-based, many low level features can be accessed directly, such as BSD sockets, POSIX threads, and DNS services.

Execution States of an Application

The iOS operating system manages the application state, but the application itself is responsible for managing important tasks to ensure smooth transition between the states. You are required to respond appropriately to application state transitions. At any given moment, your application would be in one of the below five states (Image 2).

  1. Not Running — Either the application has not started or was running and has been terminated by the system.
  2. Inactive — An application is running in the Foreground but is not receiving any events. This could happen in case a Call or Message is received. An application could also stay in this state while in transition to a different state.
  3. Active — An application is running in the Foreground and receiving the events. This is the normal mode for the Forground apps. The only way to go to or from the Active state is through the Inactive state.
  4. Background — An application is running in the background and executing the code. In addition, an application being launched directly into the background enters this state instead of the inactive state.
  5. Suspended — An application is in the background but is not executing the code. The system moves the application to this state automatically and does not notify. In case of low memory, the system may purge suspended application without notice to make free space for the foreground application. When you press the home button, or switch to another app, your app is Suspended.
Image 2

If your app is not running, the path to get to the Active state is:

Not Running → Inactive → Active

If your app is Suspended, then the path to active is:

Suspended → Background → Inactive → Active.

The path from Active to Suspended is:

Active → Inactive → Background → Suspended
State Change Flow

UIApplication object defines few set of delegate methods which are called in response to different important events in the lifetime of the application. Below are those seven most important delegate methods that you should handle in your Project.

  1. application:willFinishLaunchingWithOptions — This method is called after your application has been launched and its main storyboard or nib file has been loaded, but before your app’s state has been restored. You should use this method to initialize your application.
  2. application:didFinishLaunchingWithOptions — This method is called after state restoration is complete and before app’s window is displayed. You should use this method to complete your app’s initialization and some final adjustments.
  3. applicationDidBecomeActive — This method is either called to let your app know that it moved from the inactive to active state or your app was launched by the user or the system or in case user ignores an interruption (such as an incoming phone call or SMS message) that sent the application temporarily to the inactive state. You should use this method to restart any tasks that were paused (or not yet started) while the app was inactive.
  4. applicationWillResignActive — This method is called to let your app know that it is about to move from active to inactive state. This can happen in case of any interruptions (such as an incoming phone call or SMS message) or when the user quits the app. You should use this method to pause any ongoing tasks or disable timers etc.
  5. applicationDidEnterBackground — This method is called to let app know that it not running in the foreground. You have approximately five seconds to perform any tasks and return back. In case you need additional time, you can request additional execution time from the system by calling beginBackgroundTask(expirationHandler:). If the method does not return before time runs out your app is terminated and purged from memory.
  6. applicationWillEnterForeground — This method is called as a part of the transition from the background to the active state. You should use this to undo any change you made to your app upon entering the background. applicationDidBecomeActive method is called soon after this method has finished its execution which then moves the app from the inactive to the active state.
  7. applicationWillTerminate — This method is called to let you know that your app is about to terminate. You should use this method to perform any final clean-up task. You have approximately five seconds to perform any tasks and return back. If the method does not return before time expires, the system may kill the process altogether. This method may be called in situations where the app is running in the background (not suspended) and the system needs to terminate it for some reason.

I hope this article provided value on the iOS Architecture and Application Life Cycle. As always if you have any suggestions or questions feel free to reach out.

LinkedIn Twitter Facebook

--

--