Skip to content

Instantly share code, notes, and snippets.

@mttcrsp
Last active May 4, 2022 20:42
Show Gist options
  • Save mttcrsp/ed2cf538a16fa8aa37daed63568c093e to your computer and use it in GitHub Desktop.
Save mttcrsp/ed2cf538a16fa8aa37daed63568c093e to your computer and use it in GitHub Desktop.
UIApplication.State demo
import UIKit
// case active = 0
// case inactive = 1
// case background = 2
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
override init() {
super.init()
print(#function, UIApplication.shared.applicationState.rawValue) // 2
}
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
print(#function, application.applicationState.rawValue) // 1
return true
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
print(#function, application.applicationState.rawValue) // 1
window = UIWindow()
window?.rootViewController = UIViewController()
window?.rootViewController?.view.backgroundColor = .systemGreen
window?.makeKeyAndVisible()
return true
}
func applicationDidFinishLaunching(_ application: UIApplication) {} // is deprecated an never called
func applicationWillResignActive(_ application: UIApplication) {
print(#function, application.applicationState.rawValue, terminator: " ") // 0
DispatchQueue.main.async {
print(application.applicationState.rawValue) // 1
}
}
func applicationDidEnterBackground(_ application: UIApplication) {
print(#function, application.applicationState.rawValue) // 2
}
func applicationWillEnterForeground(_ application: UIApplication) {
print(#function, application.applicationState.rawValue, terminator: " ") // 2
DispatchQueue.main.async {
print(application.applicationState.rawValue) // 1
}
}
func applicationDidBecomeActive(_ application: UIApplication) {
print(#function, application.applicationState.rawValue) // 0
}
func applicationWillTerminate(_ application: UIApplication) {
print(#function, application.applicationState.rawValue) // 2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment