Swift Life Cycle - View Controller Lifecycle, App Lifecycle
Understanding about iOS API Lifecycle, View Controller Lifecycle and App Lifecycle
1. View Controller Lifecycle π©βπ»
1. View Controller Lifecycle
UIStoryboard
& UIKit
κ³Ό ν¨κ» μ¬μ©λμ΄, View μ Life Cycle μ κ΄λ¦¬νλ€.
μ΄κ²μ Frontend λ₯Ό ν λ Angular, React, Vue μμ μ¬μ©νλ Lifecycle Hooks μ κ±°μ λμΌν μν μ νλ€. λΈλΌμ°μ (window) κ° μλ View Components λ₯Ό κ΄λ¦¬νλ κ²μ΄ Frontend μμμ Lifecycle Hooks μΈλ° Swift μ UIViewController Lifecycle μμ μ΄μ λμΌνκ² μ± μ체μ νλ©΄ κ΄λ¦¬κ° μλ, UIStoryboard λΌλ κ²μ Life Cycle μ κ΄λ¦¬νλ€.
1 ) viewDidLoad
- View Instance κ° λ©λͺ¨λ¦¬μ λ‘λλ μ§ν νΈμΆ.
- View κ° destroyed λμ§ μλ ν 1λ²λ§ νΈμΆ.
- View μ κ΄λ ¨λ 1νμ±μ μ΄κΈ°ν μμ , λ€νΈμν¬ νΈμΆ λ±μ μ²λ¦¬.
2 ) viewWillAppear
- View κ° νλ©΄μ 보μ΄κΈ° μ§μ λ§€λ² νΈμΆ.
- View κ° destroyed λμ§ μλλΌλ λ€λ₯Έ View κ° λ³΄μ¬μ§λ€ μλ‘κ² λ³΄μ¬μ§ λλ§λ€ νΈμΆ.
- View κ° λ³΄μΌ λλ§λ€ λ§€λ² μ€νλμ΄μΌνλ μ΄κΈ°ν μμ , λ€νΈμν¬ νΈμΆ λ±μ μ²λ¦¬.
3 ) viewDidAppear
- View κ° νλ©΄μ 보μ΄κΈ° μμν λ νΈμΆ.
- View κ° νλ©΄μ 보μ¬μ§κΈ° μμνλ μμ μ μλν μ λλ©μ΄μ λ±μ μ²λ¦¬.
4 ) viewWillDisappear
- View κ° νλ©΄μμ μ¬λΌμ§κΈ° μμν λ νΈμΆ.
- View κ° μ¬λΌμ§κΈ° μ§μ μ΄κΈ°ν μμ , λ€νΈμν¬ νΈμΆ, λ°μ΄ν° μ μ₯ λ±μ μ²λ¦¬.
5 ) viewDidDisappear
- View κ° μ¬λΌμ§ λ€μ νΈμΆ.
- View Instance κ° λ©λͺ¨λ¦¬μμ μ κ±°λ μ΄ν μΆκ°μ μΈ μμ νμμ μ²λ¦¬.
2. Examples - Present Modally - Automatic
- ViewController1
class ViewController1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print("VC1 viewDidLoad Called")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("VC1 viewWillAppear Called")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("VC1 viewDidAppear Called")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
print("VC1 viewWillDisappear Called")
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
print("VC1 viewDidDisappear Called")
}
}
- ViewController2
class ViewController2: UIViewController {
@IBOutlet weak var label: UILabel!
@IBAction func goBack(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
label.text = "hello"
print("VC2 viewDidLoad Called")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("VC2 viewWillAppear Called")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("VC2 viewDidAppear Called")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
print("VC2 viewWillDisappear Called")
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
print("VC2 viewDidDisappear Called")
}
}
1 ) VC1 λ‘λ©
VC1 viewDidLoad Called
VC1 viewWillAppear Called
VC1 viewDidAppear Called
2 ) VC2λ‘ μ΄λ
VC2 viewDidLoad Called
VC2 viewWillAppear Called
VC2 viewDidAppear Called
3 ) VC1λ‘ λλμκ°κΈ°
Back
λ²νΌμ λλ¬ μ΄λμ
VC2 viewWillDisappear Called
VC2 viewDidDisappear Called
κ·Έλ°λ° Modal μ View Controller μ Life Cycle μμ μ‘°κΈ νΉμ΄ν λΆλΆμ΄ μλ€. Modal μ μ²μ²ν λμ΄ λ΄λ¦¬κ±°λ λμ΄ λ΄λ¦¬λ€ λ€μ μ¬λ¦΄ κ²½μ° νΈμΆλλ Life Cycle μ 보μ.
- Modal μ λμ΄λ΄λ¦¬λ λμ
VC2 viewWillDisappear Called
- Modal μ μμ ν λμ΄ λ΄λ € VC1μΌλ‘ μ΄λμ
VC2 viewDidDisappear Called
μ΄λ‘μ¨ Modal μ μ²μ²ν λ΄λ € λμ΄ νΈμΆλλ μμ μ΄ λͺ λ°±ν λ€λ₯΄λ€λ κ²μ νμΈν μ μλ€.
- Modal μ λμ΄ λ΄λ¦¬λ€ λ€μ μ¬λ € VC2λ‘ λλμμ¬ κ²½μ°
VC2 viewWillAppear Called
VC2 viewDidAppear Called
viewDidLoad()
λ νΈμΆλμ§ μλ κ²μ νμΈν μ μλ€.
4 ) λ€μ VC2λ‘ μ΄λ
VC2 viewDidLoad Called
VC2 viewWillAppear Called
VC2 viewDidAppear Called
5 ) μ± μΈμ΄μ¬λ¦¬κΈ°
// Nothing
6 ) μ± λ΄λ¦¬κΈ°(Background λ‘ λ³΄λ΄κΈ°)
// Nothing
μ±μ μΈμ΄ μ¬λ¦¬κ±°λ λ΄λ Έλ€ λ€μ λ€μ΄μ€λ κ²μ VC μ Lifecycle κ³Όλ 무κ΄νλ€.
3. Examples - Present Modally - Full Screen
1 ) VC1 λ‘λ©
VC1 viewDidLoad Called
VC1 viewWillAppear Called
VC1 viewDidAppear Called
2 ) VC2λ‘ μ΄λ
VC2 viewDidLoad Called
VC1 viewWillDisappear Called
VC2 viewWillAppear Called
VC2 viewDidAppear Called
VC1 viewDidDisappear Called
Modal μμ Full Screen μΌλ‘ λ°κΎΈμ μμ ν λ€λ₯Έ νλ ν¨ν΄μ 보μ¬μ€λ€.
3 ) VC1λ‘ λλμκ°κΈ°
VC2 viewWillDisappear Called
VC1 viewWillAppear Called
VC1 viewDidAppear Called
VC2 viewDidDisappear Called
λ§μ°¬κ°μ§λ‘
viewDidLoad()
λ νΈμΆλμ§ μλ κ²μ νμΈν μ μλ€. νμ§λ§ Modal κ³Ό λ¬λ¦¬ μμ ν κ°λ €μ‘λ VC1 μ λ€μ 보μ΄λ©΄μviewWillAppear(_:)
μviewDidAppear(_:)
κ° νΈμΆλλ κ²μ νμΈν μ μλ€.
4 ) λ€μ VC2λ‘ μ΄λ
VC2 viewDidLoad Called
VC1 viewWillDisappear Called
VC2 viewWillAppear Called
VC2 viewDidAppear Called
VC1 viewDidDisappear Called
5 ) μ± μΈμ΄μ¬λ¦¬κΈ°
// Nothing
6 ) μ± λ΄λ¦¬κΈ°(Background λ‘ λ³΄λ΄κΈ°)
// Nothing
μ€μν κ²μ λ λ€ λ³΄μ¬μ£Όλ λ°©μμ μ°¨μ΄μΌ λΏ λͺ¨λ Modal λ°©μμ μ¬μ©νκΈ° λλ¬Έμ VC1 μ VC2 λ‘ κ°λλΌλ μλ κ°μΆ°μ§λ κ²μΌ λΏ destroyed λμ§ μκ³
viewDidLoad()
κ° λ€μ νΈμΆλμ§ μλλ€. λ°λ©΄ VC2 λ VC1 λ‘ λμκ° λ destroyed λκΈ° λλ¬Έμ VC2 λ‘ λ€μ μ΄λν κ²½μ°viewDidLoad()
κ° λ€μ νΈμΆλλ€.
2. App Lifecycle π©βπ»
1. App Lifecycles
μ±μ νμ¬ μνμ λ°λΌ μΈμ λ μνν μ μλ μμ
κ³Ό μνν μ μλ μμ
μ΄ κ²°μ λλ€. μλ₯Ό λ€λ©΄ Foreground App
μ CPU λ₯Ό ν¬ν¨ν μμ€ν
리μμ€λ³΄λ€
μ°μ μμκ° λλ€. λ°λλ‘ Background App
μ κ°λ₯ν ν μ μ μμ
μ μνν΄μΌνλ©°, νλ©΄ λ°μ μκΈ° λλ¬Έμ κ°κΈμ μ무 μμ
λ μννμ§ μλ κ²μ΄ μ’λ€.
μ±μ μν λ³νλ₯Ό κ°μ§νκ³ κ·Έμ λ°λ₯Έ μλμ μ‘°μ νκΈ° μν΄ Swift μμλ λ€μκ³Ό κ°μ λ°©λ²μ μ 곡νλ€.
- UISceneDelegate : iOS 13 μ΄μμμ App μ Lifecycles λ₯Ό κ΄λ¦¬νκΈ° μν΄ μ¬μ©νλ€.
- UIApplicationDelegate : iOS 12 μ΄νμμ App μ Lifecycles λ₯Ό κ΄λ¦¬νκΈ° μν΄ μ¬μ©νλ€.
document.addEventListener('visibilitychange', (event) => {
if (document.hidden) {
console.log('not visible');
} else {
console.log('is visible');
}
});
iOS μμ μ±μ μνμ λ°λΌ νλμ μ μ΄νλ€λ κ²μ Frontend μμ λΈλΌμ°μ μ κ·Έ νμ νμ± μνλ₯Ό κ°μ§νκ³ μ΄μ λ°λ₯Έ μλμ μ‘°μ νλ κ²κ³Ό κ°λ€.
1 ) UISceneDelegate
2 ) UIApplicationDelegate
μλ μ½λμμ λ³Ό μ μλ―μ΄ UISceneDelegate
λ UIApplicationDelegate
μ
application(_:didFinishLaunchingWithOptions:
) λ©μλμ application(_:configurationForConnecting:options:)
λ©μλ μ¬μ΄μ
μμΉνλ€.
2. Examples - Present Modally - Automatic
- AppDelegate: UIResponder, UIApplicationDelegate
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
print(#function)
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
print(#function)
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
print(#function)
}
}
- SceneDelegate: UIResponder, UIWindowSceneDelegate
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
print(#function)
guard let _ = (scene as? UIWindowScene) else { return }
}
func sceneDidDisconnect(_ scene: UIScene) {
print(#function)
}
func sceneDidBecomeActive(_ scene: UIScene) {
print(#function)
}
func sceneWillResignActive(_ scene: UIScene) {
print(#function)
}
func sceneWillEnterForeground(_ scene: UIScene) {
print(#function)
}
func sceneDidEnterBackground(_ scene: UIScene) {
print(#function)
}
}
1 ) VC1 λ‘λ©
application(_:didFinishLaunchingWithOptions:)
scene(_:willConnectTo:options:)
VC1 viewDidLoad Called
VC1 viewWillAppear Called
sceneWillEnterForeground(_:)
sceneDidBecomeActive(_:)
VC1 viewDidAppear Called
μ±μ μμν¨κ³Ό λμμ App Life Cycles application(_:didFinishLaunchingWithOptions:)
κ³Ό scene(_:willConnectTo:options:)
μ΄
νΈμΆλλ€.
κ·Έλ¦¬κ³ μ²« λ²μ§Έ View κ° λ¨κ³ λ‘λ λμ΄ λ¨κΈ° μ μ±μ΄ Foreground
λ‘ μ§μ
νκ² λκ³ , UISceneDelegate κ° μ΄λ₯Ό κ°μ§νκ³
sceneWillEnterForeground(_:)
μ sceneDidBecomeActive(_:)
λ₯Ό νΈμΆνλ€.
2 ) VC2λ‘ μ΄λ
VC2 viewDidLoad Called
VC2 viewWillAppear Called
VC2 viewDidAppear Called
μ±μ κ³μν΄μ Foreground
λ₯Ό μ μ§νλ―λ‘, App Life Cycle μ λ³νκ° μλ€.
3 ) VC1λ‘ λλμκ°κΈ°
Back
λ²νΌμ λλ¬ μ΄λμ
VC2 viewWillDisappear Called
VC2 viewDidDisappear Called
λ§μ°¬κ°μ§λ‘ App Life Cycle μ λ³νκ° μλ€.
4 ) μ± μΈμ΄μ¬λ¦¬κΈ°
sceneWillResignActive(_:)
5 ) μ± λ΄λ¦¬κΈ°(Background λ‘ λ³΄λ΄κΈ°)
sceneDidEnterBackground(_:)
6 ) μ± λ€μ λΆλ¬μ€κΈ°(Foreground λ‘ λΆλ¬μ€κΈ°)
sceneWillEnterForeground(_:)
sceneDidBecomeActive(_:)
7 ) μ± μλ‘ λ λ € μ’ λ£νκΈ°
sceneWillResignActive(_:)
sceneDidDisconnect(_:)
application(_:didDiscardSceneSessions:)
VC1 viewWillDisappear Called
VC1 viewDidDisappear Called
3. Examples - Present Modally - Full Screen
1 ) VC1 λ‘λ©
application(_:didFinishLaunchingWithOptions:)
scene(_:willConnectTo:options:)
VC1 viewDidLoad Called
VC1 viewWillAppear Called
sceneWillEnterForeground(_:)
sceneDidBecomeActive(_:)
VC1 viewDidAppear Called
2 ) VC2λ‘ μ΄λ
VC2 viewDidLoad Called
VC1 viewWillDisappear Called
VC2 viewWillAppear Called
VC2 viewDidAppear Called
VC1 viewDidDisappear Called
3 ) VC1λ‘ λλμκ°κΈ°
Back
λ²νΌμ λλ¬ μ΄λμ
VC2 viewWillDisappear Called
VC1 viewWillAppear Called
VC1 viewDidAppear Called
VC2 viewDidDisappear Called
4 ) μ± μΈμ΄μ¬λ¦¬κΈ°
sceneWillResignActive(_:)
5 ) μ± λ΄λ¦¬κΈ°(Background λ‘ λ³΄λ΄κΈ°)
sceneDidEnterBackground(_:)
6 ) μ± λ€μ λΆλ¬μ€κΈ°(Foreground λ‘ λΆλ¬μ€κΈ°)
sceneWillEnterForeground(_:)
sceneDidBecomeActive(_:)
7 ) μ± μλ‘ λ λ € μ’ λ£νκΈ°
sceneWillResignActive(_:)
sceneDidDisconnect(_:)
application(_:didDiscardSceneSessions:)
VC1 viewWillDisappear Called
VC1 viewDidDisappear Called
Reference
- Angela Yu, βiOS & Swift - The Complete iOS App Development Bootcamp, Section 15.β Udemy.com. last modified Nov. 2021, https://www.udemy.com/course/ios-13-app-development-bootcamp/.
- βUIViewController.β Apple Developer Documentation. accessed Mar. 16, 2023, UIViewController Life Cycle.
- βManaging your appβs life cycle.β Apple Developer Documentation. accessed Mar. 16, 2023, App Life Cycle.