# [금주의 실험🔥] Life Cycle 통합 실험 # 👨‍🔬 실험 목표 > 프로젝트 내부에는 두 개의 ViewController 파일이 구성되어 있습니다. 우리는 ViewController가 전환되는 순간 벌어지는 일에 대해서 깊이 있게 이해할 필요가 있습니다. View Life Cycle과 관련한 메서드를 재정의하여, 각 메서드가 언제, 어떤 상황에서 호출되는지 확인해봅시다. # 📋 Todo ## 각 상황에서 View-related Notifications 은 어떻게 변화할까요? ### 첫 화면이 보여지는 경우 ``` First - ViewDidLoad First - ViewWillAppear First - ViewDidAppear ``` ### 첫 화면에서 두번째 화면으로 이동하는 경우 <details> ## ⚫️ 두 번째 화면이 일부 화면만 덮는 경우 <image src = "https://user-images.githubusercontent.com/45350356/181864351-3aeef9f0-08d5-4a93-8413-b9849a5c81c1.gif" width="250" height="500"> ``` Second - ViewDidLoad Second - ViewWillAppear Second - ViewDidAppear ``` #### - 두 번째 화면을 밑으로 살짝 내렸을 때 , 대기상태 <image src = "https://user-images.githubusercontent.com/45350356/181864457-db10346a-61cb-46c1-b4f9-c76fcdad622c.gif" width="250" height="500"> ``` Second - ViewWillDisappear ``` #### 1. 다시 덮는 경우 <image src = "https://user-images.githubusercontent.com/45350356/181864545-a41e7d52-0618-46c2-8232-390c0074075f.gif" width="250" height="500"> ``` Second - ViewWillAppear Second - ViewDidAppear ``` #### 2. 내려버리는 경우 ``` Second - ViewDidDisappear ``` ## ⚫️ 두 번째 화면이 전체 화면을 덮는 경우 ### fullScreen인 경우 - 첫화면이 나타날 때, ``` First - ViewDidLoad First - ViewWillAppear First - ViewDidAppear ``` - Second 뷰로 넘어갈 때, ``` Second - ViewDidLoad First - ViewWillDisappear Second - ViewWillAppear Second - ViewDidAppear First - ViewDidDisappear ``` - First 뷰로 뒤로 돌아갈 때, ``` Second - ViewWillDisappear First - ViewWillAppear First - ViewDidAppear Second - ViewDidDisappear ``` ### overFullScreen인 경우 present Modally 기본일 때와 같지만, 뒤로 돌아가는 버튼이 없다 - 첫화면이 나타날 때, ``` First - ViewDidLoad First - ViewWillAppear First - ViewDidAppear ``` - Second 뷰로 이동할 때, ``` Second - ViewDidLoad Second - ViewWillAppear Second - ViewDidAppear ``` - First 뷰로 뒤로 돌아갈 때, ``` Second - ViewWillDisappear Second - ViewDidDisappear ``` </details> ### 각 View-related Notifications 관련 메서드에는 어떤 코드를 작성하는 것이 좋을까요? # 👨‍🔬 실험 2: App(Scene)의 상태 이해하기 > 우리는 App이 Foreground에 있을 때, Background에 있을 때, 종료 되었을 때 등, 각 상황에 맞는 동작을 마련해둘 필요가 있습니다. 그리고 각 상황을 이해하기 위해 필요한 첫 걸음은 App(Scene)의 상태를 이해하는 것입니다. 각 상황 속에서 App(Scene)의 상태가 어떻게 변화하는지를 추리해봅시다. 실험 2에서는 아티클을 읽고 아래 상태 변화를 먼저 추론한 후 검증해봅시다. (iOS 13이후 Scene Based Life Cycle을 기준으로 합니다.) https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle # 📋 Todo > App(Scene)의 상태에는 무엇이 있는지 확인해봅시다. 각 상황에서 App의 상태는 어떻게 변화할지 떠올려봅시다. ### 핸드폰을 처음 켜고 App을 실행했을 때 ```swift func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { } ``` ### App 실행 화면에서 멀티태스킹 화면으로 이동했을 때 ``` SceneDelegate - sceneWillResignActive call ``` ### App 실행 화면에서 이전 앱으로 전환했을 때 ``` SceneDelegate - sceneWillResignActive call SceneDelegate - sceneDidEnterBackground call ``` ### 앱 실행 중 전화 알림이 왔을 때, 상단 바에 알림창이 떠있는 경우 (앱 전체를 안가리는 경우) ``` 아무것도 호출 안됨 ``` ### 앱 실행 중 전화 알림이 왔을 때, 전화 화면이 전체 화면으로 뜨는 경우 (앱 전체를 가리는 경우) ``` SceneDelegate - sceneDidEnterBackground call SceneDelegate - sceneWillEnterForeground call ``` >추론한 바와 같은지 검증해봅시다. 실험 1의 각 상황을 시뮬레이터 or 기기 로 진행해보면서, 어떤 메서드가 호출되는지, 호출 순서가 어떻게 되는지 확인해봅시다. ### 앱 전환 시 Activate 관련 메서드와 Ground 관련 메서드는 각각 어떤 순서로 호출되나요? - 다른 앱으로 전환시 ``` SceneDelegate - sceneWillResignActive call SceneDelegate - sceneDidEnterBackground call ``` - 앱으로 돌아올시 ``` SceneDelegate - sceneWillEnterForeground call SceneDelegate - sceneDidBecomeActive call ``` ### App의 상태가 변화할때 View Life Cycle과 관련한 메서드는 호출되는지 알아봅시다. ``` AppDelegate - didFinishLaunchingWithOptions launchOptions SceneDelegate - connectionOptions: UIScene.ConnectionOptions call 💻 MainViewController - viewDidLoad 💻 MainViewController - viewWillAppear SceneDelegate - sceneWillEnterForeground call 💻 MainViewController - viewDidAppear SceneDelegate - sceneDidBecomeActive call ``` # 추가 학습활동 ### loadView를 재정의해서 rootView를 직접 만들어보기 [릴리 블로그](https://velog.io/@yeahg_dev/loadView) - 커스텀 뷰를 루트 뷰로 설정하고 싶은 경우 loadView를 재정의 해서 사용하면 된다. ### 카카오톡의 다음과 같은 기능은 어떻게 구현해볼 수 있을까요? ### 비밀번호를 건 채 백그라운드에 나간 경우 https://s3.ap-northeast-2.amazonaws.com/media.yagom-academy.kr/resources/usr/6131c8fa2e11413823f8dd7f/20220719/62d5f2e1411315485598974b.png