APPDelegate

2023. 12. 3. 14:10iOS

APPDelegate
- 앱의 공유 동작을 관리하는 메서드 집합체!

 

 12와 13의 차이
- UILifecycle 관리의 차이가 있다.

- 12 AppDelegate
    - 12까지 AppDelegate에서는 UILifecycle까지 관리하였다.
- 13 AppDelegate
    - UILifeCycle은 13이후로 AppDelegate에서 하지않고 Scene Delegate로 이동하여 관리를 진행하게된다.

1. iOS12 까지는 대부분의 앱에 하나의 window였지만 iOS13 부터는 window의 개념이 scene으로 대체되고 하나의 앱에서 여러개의 scene을 가질 수 있습니다.
2. AppDelegate의 역활중 UI상태를 알 수 있는 UILifeCycle에 대한 부분을 SceneDelegate가 하게 되었습니다.
3. AppDelegate에 Session lifecycle에 대한 역할이 추가 됐습니다. Scene Session이 생성되거나 삭제 될때 AppDelegate에 알리는 두 메소드가 추가 되었습니다.  Scene Session은 앱에서 생성한 모든 scene의 정보를 관리합니다.

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
UIApplicationDelegagte UISceneDelegate
x(이동 ->) o(생김)
application:willEnterForeground| scene:willEnterForground
application:didEnterBackground| scene:didEnterBackground
application:willResignActive scene:willResignActive
application:didBecomeActive scene:didBecomeActive



앱의 라이프싸이클넘겨놓고 이제 넌 뭐해??
iOS13부터 AppDelegate가 하는일
1. 앱의 가장 중요한 데이터 구조를 초기화 하는것??
2. 앱의 scene을 환경설정(configuration)하는것
3. 앱 밖에서 발생한 알림(배터리, 다운로드)에 대응
4. 애플 푸쉬 알림 서비스와 같이 실행시 요구되는 모든 서비스를 등록하는것.

앱 상태

1. Not Running 
    - 앱이 실행되지 않았거나, 완전히 종료되어 동작하지 않을때

2. Inactive(Foreground)
    - 앱이 실행 되면서 foreground에 진입하지만, 어떠한 이벤트도 받지않는 상태 앱의 상태 전환 과정에서 잠깐 머무는 단계

3. Active(Foreground)
    - 앱이 실행 중이며, foreground에 있고, 이벤트를 받고 있는 상태

4. Background
    - 앱이 백그라운드에 있으며 다른 앱으로 전환되었거나 홈버튼을 눌러 밖으로 나갔을때 상태

5. Suspended
    - 앱이 Background상태에 있지만, 아무 코드도 실행하지않는상태
    - 백그라운드에서 특벽한 작업이 없을 경우 Suspended상태가된다.

 

AppDelegate 객체의 메소드 호출


- Not Runung 상태 일때

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }
앱 실행을 위한 모든 준비가 끝난 후 화면이 사용자에게 보여지기 직전에 호출됩니다.


이거 하나 사용하고 나머지 상태일때는 다 scene에서 쓰는거같다.

- 추가적으로 앱델이게이트에서 쓸수 있는 메서드이다. 기본적으로 코드가 작성되어있지않기에 추가해주면된다.

applicationWillResignActive: 앱이 active에서 inactive로 이동될때 사용

applicationDidEnterBackground: 앱이 backgrund 상태일 때 실행

applicationWillEnterForeground: 앱이 background에서 foreground로 이동 될때실행

applicationDidBecomeActive: 앱이 active 상태가 되어 실행 중일때

apllicationWillTerminate: 앱이 종료될때

 

 

얼추 마무리 단계로 넘어 간다면?
- iOS12까지는 하나의 앱이 하나의 윈도우(window)만 가지기 때문에 AppDelegate클래스가 UI생명주기 관리까지 했습니다.
- 하지만 iOS13부터 하나의 앱에 여러 개의 윈도우(window)를 동시에 사용할 수  있게 되었기 때문에 UI생명주기를 전담 관리해줄 클래스가 필요해졌다. 그렇기에 SceneDelegate가 생겼습니다~

- window 뭔데? 당신이 대장인가요??
    - 사용자 인터페이스의 기본적인 컨테이너이다. 13부터 SceneDelegate를 통해서 다중 창 환경에 있어 효율적으로 관리를 할 수 있다.
    - SceneDelegate는 다중 창 환경에서 각각의 Scene(화면)에 대한 라이프사이클 관리 및 설정을 담당합니다. 각 Scene은 하나의 UIWindow와 연결되어 있을 수 있습니다. 따라서 SceneDelegate는 앱의 여러 창에서 각각의 창에 대한 UI와 상태를 관리합니다.

 

 

 

'iOS' 카테고리의 다른 글

OOP (객체 지향 프로그래밍)  (1) 2024.01.08
POP(프로토콜 지향 프로그래밍)  (0) 2024.01.08
Cache  (0) 2023.08.30
메모리 구조 와 ARC  (0) 2023.08.30
SOLID  (0) 2023.08.22