나의 기록, 현진록

[iOS] App Launch Sequence 본문

iOS

[iOS] App Launch Sequence

guswlsdk 2025. 2. 20. 15:50
반응형

https://developer.apple.com/documentation/uikit/about-the-app-launch-sequence

 

About the app launch sequence | Apple Developer Documentation

Learn the order in which the system executes your code at app launch time.

developer.apple.com

 

 

 

 

 

 

 

 

  1. 사용자 또는 시스템이 앱을 실행하거나 시스템이 앱을 사전 준비(prewarm)한다. 
    1. iOS 15이후 버전에서는 시스템은 디바이스 상태에 따라서 앱을 사전 준비 시킬 수 있다. 앱이 사용 가능한 상태가 되기 전까지 사용자가 기다리는 시간을 최소화하기 위해 실행 되지 않은(norunning) 애플리케이션 프로세스들을 실행한다.
    2. Low-Level 구조를 구축하고 캐싱하는 역할을 한다.
  2. 시스템은 Xcode가 제공하는 main() 메서드를 실행한다.
  3. main() 메서드는 UIApplication과 AppDelegate 인스턴스를 생성하는 UIApplicationMain() 함수를 호출한다.
    1. 앱 마다 하나의 UIApplication이 있고 iOS 시스템과 App과의 상호작용 할 수 있도록 AppDelegate에게 위임한다.
  4. UIKit은 Info.plist 파일이나 Xcode project editor의 Custom iOS Target Properties 탭에 명시된 기본 스토리보드를 로드한다. 만약 스토리보드를 사용하지 않는다면 이 과정은 스킵된다.
    1. 스토리보드 사용 여부에 대해서는 Info.list의 "Main storyboard file base name" 항목을 체크하여 있을 경우 스토리보드를 Main Interface로 로드한다.
    2. 스토리보드가 있을 때 UIKit은 window와 ViewController를 만들어준다. UIWindow는 UIView의 가장 상위 계층에 위치하게 되고 앱이 종료되기 전까지 유지한다. 
    3. window가 디바이스 화면과 같은 사이즈가 되도록 screen bounds에 window frame을 맞춘다.
    4. ViewController가 window의 rootViewController가 될 수 있도록 설정하며 window의 subView는 viewController에서의 self.view가 된다.
    5. UIKit이 window.makeKeyAndVisible() 메서드를 호출하여 window를 key window로 지정한다.
  5. UIKit은 AppDelegate의 application(_:willFinishLaunchingWithOptions:) 메서드를 호출한다.
    1. 앱이 실행되기 전 필요한 설정을 수행할 수 있음
  6. UIKit은 상태 복원을 수행하여 AppDelegate와 viewController의 추가적인 메서드를 실행한다.
  7. UIKit은 AppDelegate의 application(_:didFinishLaunchingWithOptions:) 메서드를 호출한다.
    1. 앱이 완전히 실행된 후 추가적인 설정을 수행할 수 있음
    2. 푸시 알림 등록, firebase 설정 등

이러한 과정이 완료되면 시스템은 SceneDelegate를 사용하여 앱의 UI를 나타내고 생명 주기(Life Cycle)를 관리한다.

 

 

 

스토리보드 없이 사용할 때

4번 과정이 스킵되고 5과 7번 과정이 실행된다.

스토리보드를 사용하지 않으므로 UIKit이 자동으로 window을 생성하지 않는다.

7번 과정에서 application(_:didFinishLaunchingWithOptions:)에서 window를 직접 생성하고 설정해야 한다.

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds) // window 생성
        window?.rootViewController = ViewController() // 루트 ViewController 설정
        window?.makeKeyAndVisible() // key window로 설정하고 화면에 표시

        return true
    }
}

 

window를 screen 크기에 맞게 설정하고

window의 rootVC를 설정해주고

window의 key window를 설정해준다.

 

UIKit이 자동으로 생성해주었던 작업을 직접 설정하면 된다.

반응형

'iOS' 카테고리의 다른 글

[iOS] WKWebView로 웹뷰 만들기  (0) 2025.03.11
[Swift] RxSwift  (0) 2025.02.24
[iOS] UIView & CALayer  (0) 2025.02.18
[iOS] iOS 뷰 계층 구조  (0) 2025.02.17
[SwiftUI] @ObservedObject @StateObject 관련 트러블 슈팅  (0) 2024.11.27