01 logo

iOS Native Development - Application life cycle

Different states of iOS application

By ConficlePublished 3 years ago 3 min read
3

Hello, we are back with our next article in a series of native iOS development. In case you would like to refer to our previous articles, you can find them here.

In this article we will explore different states of an application life cycle.

In the iOS app lifecycle there are five different states.

1. Not running: This is the state when the app is just installed on the phone and not running.

2. Active: The moment a user launches the app it goes from not running to an active state. It’s in the forefront of our device we can interact with it.

3. Inactive: This is the state in which your application is in the background. Application can go in an inactive/background state when the user single presses the home button or any other application like a phone call takes precedence over your app.

4. Background: In background state some app may still be doing things such as Music or messaging app like WhatsApp

5. Suspended: Apps that don’t do anything for a long time are moved to this state. There is no activity done by the app but still the app remains in memory.

With respect to above states below methods get called from the app delegate file(will have a separate article on what is this app delegate file).

To see how program/ application flow executes simply create new Xcode project and refer AppDelegate.swift and apply breakpoint to below methods and debug

Once you open AppDelegate.Swift file you can see below default set of methods where we have added print statement

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

print("Application will finish launching called")

return true

}

func applicationWillResignActive(_ application: UIApplication) {

print("Application will resign active called")

}

func applicationDidEnterBackground(_ application: UIApplication) {

print("Application did enter background called")

}

func applicationWillEnterForeground(_ application: UIApplication) {

print("Application will enter foreground called")

}

func applicationDidBecomeActive(_ application: UIApplication) {

print("Application did become active called")

}

func applicationWillTerminate(_ application: UIApplication) {

print("Application will terminate called")

}

applicationDidFinishLaunchingWithOptions: Very first method to be executed once your application is launched or open. Perform all application level initializations in this method. For example initializing a database, an api etc.

applicationWillResignActive: Lets you know that your app is transitioning away from foreground state. Use this method to put your app into an inactive state.

applicationDidEnterBackground: As the name suggests, the application is now in the background. Use this method to perform operations like saving user data or preferences.

applicationWillEnterForeground: Lets you know that your app is moving out of background and back into foreground.

applicationDidBecomeActive: As the name suggests, this method is called when application becomes active.

applicationWillTerminate: Lets you know that application will be terminated or killed. If the application is in Suspended state then this method doesn’t get called.

Now let's relate with a real life example. User has Weather app on his phone and below are the use cases and respective methods ( Which gets called in code behind )

User taps on weather application: Immediately didFinishLaunchingWithOptions methods get called and on click of “Continue to program execution” button control goes to initial view controller and viewDidLoad methods get called and immediately applicationDidBecomeActive gets called successively

Application did finish launching called - A

View did load of initial viewController get called

Application did become active called - B

User presses the home button or gets a phone call: As phone call has more priority, application goes to inactive states from active. This time applicationWillResignActive and applicationDidEnterBackground gets called successively

Application will resign active called - C

Application did enter background called - D

User taps on app or done with phone call:

Application will enter foreground called - E

Application did become active called - B

User closes the app from the background by double pressing the home button and swiping: applicationWillTerminate method gets called and execution stops.

Application will terminate called - F

That’s it for this article. We will bring more regarding project structure, view controller life cycle in our next articles. Till then please stay tuned.

Thanks for reading this article. If you have any queries related to this topic or iOS, Objective C and Swift. Please write us at [email protected] or direct message us on instagram at conficle(instagram username).

Also keep watching this space for upcoming articles on iOS development, software development and technology concepts.

apps
3

About the Creator

Conficle

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.