The iOS Application’s States & Life Cycle Methods
Every iOS Developer should aware of the application lifecycle. It helps to make enriched, immersive, and smooth User experience.
When we create an application with Xcode, with lots of other stuff and files, a file AppDelegate.swift gets generated automatically. AppDelegate.swift plays the primary role in application development.
AppDelegate is the application delegate object. It inherits the UIResponder class and implements the UIApplicationDelegate delegate protocol. The UIResponder class can make AppDelegate have the ability to response events, and UIApplicationDelegate enables the AppDelegate to be an application delegate object to manage and respond to the life cycle of the application.
1. iOS Application Life Cycle States
At any given moment, our app fells into one of the below-given states.
Not Running: App has not been launched or terminated by the system.
Inactive: The app is running in the foreground but is currently not receiving any event. This could happen in case a Call or Message is received. An application could also stay in this state while in the transition to a different state. In this State, we can not interact with the app’s UI.
Active: App is running in the foreground and receiving events. This is the normal mode for the Foreground apps. The only way to go to or from the Active state is through the Inactive state. A user normally interacts with UI and can see the response/result for user actions.
Background: App is in the background and executing code. Freshly launched apps directly enter into In-Active state and then to Active state. Apps that are suspended, will come back to this background state, and then transition to In-Active → Active states.
Suspended: App is in the background but not executing 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. Usually, after 5 secs spent in the background, apps will transition to Suspend state, but we can extend the time if the app needs.
2. iOS Application Life Cycle Methods
The AppDelegate class implements the required behavior of an application.
AppDelegate class defines some methods which are called or will be responded to some of the above states.
application:willFinishLaunchingWithOptions: This method is called after our application has been launched successfully. It is the first method from our app delegate, which gets called. We can execute our code if the launch was successful.
application:didFinishLaunchingWithOptions: This method is called before the app’s window is displayed. We can finalize our interface and can provide the root ViewController to the window.
applicationDidBecomeActive: This method is either called to let our app know that it moved from the inactive to active state or our 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. We should use this method to restart any tasks that were paused (or not yet started) while the app was inactive.
applicationWillResignActive: This method is called to let our 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 Calendar alerts) or when the user quits the app. We should use this method to pause any ongoing tasks or disable timers etc.
applicationDidEnterBackground: This method is called to let the app know that it is not running in the foreground. We have approximately five seconds to perform any task and return back. In case we need additional time, we 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. applicationWillResignActive method is called before this method.
applicationWillEnterForeground: This method is called as a part of the transition from the background to the active state. We should use this to undo any change we made to our 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.
applicationWillTerminate: This method is called to let our app know that it is about to terminate. We should use this method to perform any final clean-up task. 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. We shouldn’t wait for applicationWillTerminate to be called in order to save our data. There are some cases when applicationWillTerminate won’t be called before app termination. For example, the system will not call applicationWillTerminate when the device reboots.