iOS App lifecycle is the most important and the most basic flow which every iOS developer should understand in depth to make sure he uses the power of lifecycle methods coherently as apple directs. In this post we will understand application life cycle in ios and I’ll explain the same in depth.
What is iOS Application lifecycle ?? — An application which is written in iOS Framework have to traverse through multiple states as it runs. These states are known as state’s of application lifecycle.
Every iOS application will transitioned into following states:
- Not Running
How does the application start?? — As soon as user taps on the application icon, Springboard( Manages iOS home screen, starting WindowServer, launching and bootstrapping applications) launches the application, eventually your application begins its execution and application delegate receives the notifications.
If you’re coming from C or Java background you know application execution starts from main, Similarly here we have @UIApplicationMain attribute in AppDelegate.swift file which will automatically internally call the UIApplicationMain. UIApplicationMain is the replacement of main.m file (Objc-C) and is an entry point of your application.
Now let’s look into the states of application in depth…
Not Running: The application is not yet started or is terminated by the system.
Inactive: When an application starts it transitions through a short state called Inactive state. It’s actually running but it is not ready to accept the user input as it is performing other functions. This happens when a call or SMS is received.
Active: An application is in the active state running in the foreground and receiving the events.
Background and Suspended: When the application is in the background its UI isn’t visible but it is running, The iOS application move an app to suspended state. Here the application is in the background but not running any code. It does stay in memory though, a low memory condition occur the system may purge the application in the suspended state without notice. According to apple’s standard only iOS System can kills the application
Now will look into the lifecycle methods and relate them with the states.
- application: willFinishLaunchingWithOptions: -> Bool — This method is intended for initial set up, this method is called after your app has been launched and its main.storyboard or nib file has been loaded, but before your app’s state has been restored. At the time this method is called your app is in inactive state.
- application: didFinishLaunchingWithOptions: -> Bool — This method is called when the application is ready to run. This method is called when the app state restoration has occurred but before your app’s window and other UI is presented.
Both the methods can potentially be launched with options identifying that the app was called to handle the Push notification or the URL, you need to return true if your app can handle the given activity or URL.
Transitioning to Foreground:
- applicationWillEnterForeground: This method is called when transition from background state to active state occurs.
- applicationDidBecomeActive: This method is called when the app is moved from inactive to active state. You should use this method to restart any tasks that were paused or not started yet while the application was inactive. If your application was in background you could use this to refresh the UI.
Transitioning to Background:
- applicationDidEnterBackground: This is called when your application enters the background after becoming inactive. You get approximately 5 seconds to run any tasks you need to back things up in case the app gets terminated later or right after that.
Transitioning to Inactive State:
- applicationWillResignActive: This method is called when the application is about to become inactive, e.g. A call comes or user presses home button.
- applicationWillTerminate: This method is called when your application is about to be purged from the memory. Developer should call final cleanup here.
So this is all about iOS Application Lifecycle. In this tutorial you would’ve come across the word delegate !!! Now this word scares a lot of developers initially and yes it did scared me too…. The next post is all about Delegates and Protocols where we will deep dive in those concept and understand it in depth.
Do let me know if you have any doubts regarding application life cycle in ios in the comment section.