[Swift UI] How to get the startup status of the application [iOS]

This is the first post! I was developing an iOS app with SwiftUI, and I had a hard time finding out how to get the launch status of the app, so I will share it here as a memorandum.

About the startup status in the iOS app

The startup state of the iOS app can be divided into three states: Active, Inactive, and Background. The state transition diagram is as shown in the figure below. You can see that when you start it, it goes through Inactive and then becomes Active, and when it goes from Active to Background, it goes through Inactive.

See Apple Official Documentation for more information.

How to get in Swift UI

Until recently, Swift UI did not have an API to get the startup status of the app, but from iOS14 it can be easily obtained by using Scene Phase .

Specifically, you can get it by adding the following property to the App instance.

@Environment(\.scenePhase) private var scenePhase

Let's look at an example. Rewrite TestApp.swift, which is automatically generated when you launch a new project in Xcode, as follows. (In this case, Test is the project name)

//
//  TestApp.swift
//  Test
//
//  Created by Ossamoon on 2020/12/25.
//

import SwiftUI

@main
struct TestApp: App {
    @Environment(\.scenePhase) private var scenePhase

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .onChange(of: scenePhase) { phase in
            switch phase {
            case .background:
 print ("Background")
            case .active:
 print ("active")
            case .inactive:
 print ("inactive")
            default:
 print ("Something is strange if this message appears")
            }
        }
    }
}

Let's start the app in the simulator. Then, the message "Active" appears on the console.

If you swipe from here to display the menu, the message "Inactive" will appear.

If you go to another screen, the message "Background" will appear.

If you return to the app screen again, the message "Inactive" will be displayed, followed by the message "Inactive".

It behaves like this. I hope you can actually play around with it at your fingertips.

Summary

You can see that you can easily get the launch status of the app by using ScenePhase.

There is a restriction that it can be used only with iOS 14 or later, but it is a very convenient API, so please try Scene Phase!

References

Recommended Posts

[Swift UI] How to get the startup status of the application [iOS]
[Swift] How to get the document ID of Firebase
How to get today's day of the week
[Swift UI] How to disable ScrollsToTop of ScrollView
[Java] How to get the authority of the folder
[Swift] How to get the number of elements in an array (super basic)
[Java] How to get the URL of the transition source
How to get the ID of a user authenticated with Firebase in Swift
[Java] How to get the maximum value of HashMap
[Android] How to get the setting language of the terminal
[Rails] How to get the contents of strong parameters
How to get the longest information from Twitter as of 12/12/2016
How to change BackgroundColor etc. of NavigationBar in Swift UI
[jsoup] How to get the full amount of a document
[Swift5] How to analyze complex JSON and get the index of the element that satisfies the condition
[Rails] How to get the URL of the transition source and redirect
[Swift] How to implement the Twitter login function using Firebase UI ①
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
How to get the contents of Map using for statement Memorandum
How to get the id of PRIMAY KEY auto_incremented in MyBatis
[Swift] How to implement the Twitter login function using Firebase UI ②
How to get the length of an audio file in java
[Swift UI] How to get the startup status of the application [iOS]
[Swift] Acquisition of IDFA on iOS14
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
[IOS] How to get data from DynamoDB
How to determine the number of parallels
[Java] How to get the current directory
[Swift] How to implement the countdown function
[Swift] Get the height of Safe Area
How to sort the List of SelectItem
How to get the date in java
Get the event that the iOS application moves to the back on the View side
[Java] How to get to the front of a specific string using the String class
How to get the absolute path of a directory running in Java
[swift5] How to change the color of TabBar or the color of item of TabBar with code
Customize how to divide the contents of Recyclerview
SKStoreReviewController implementation memo in Swift UI of iOS14
[Swift] How to implement the LINE login function
[swift5] How to implement the Twitter share function
How to add sound in the app (swift)
[Swift] How to link the app with Firebase
How to delete the database when recreating the application
Output of how to use the slice method
[Swift] How to implement the fade-in / out function
How to display the result of form input
[Java] How to get the redirected final URL
[swift] How to control the behavior when the back button of NavigationBar is pressed
graphql-ruby: How to get the name of query or mutation in controller Note
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
[Swift] How to change the order of Bar Items in Tab Bar Controller [Beginner]
[Java] How to easily get the longest character string of ArrayList using stream
How to set environment variables in the properties file of Spring boot application
[Swift] Use Swity JSON to easily get information from JSON data of the fortune-telling API
[Swift] Get the number of steps with CMP edometer
How to delete / update the list field of OneToMany
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
How to write Scala from the perspective of Java
[Ruby] How to find the sum of each digit
How to install the root certificate of Centos7 (Cybertrust)
How to use UsageStatsManager in Android Studio (How to check the startup time of other apps)
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
[SwiftUI] How to specify the abbreviated position of Text