본문 바로가기
iOS/설명

[iOS] 상황별 app life cycle 메서드 호출 순서

by Sky Titan 2022. 6. 6.
728x90
@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        window = UIWindow(frame: UIScreen.main.bounds)
        
        let vc = ViewController()
        let navVC = UINavigationController(rootViewController: vc)
        navVC.isNavigationBarHidden = true
        window?.rootViewController = navVC
        window?.makeKeyAndVisible()
        
        print("application didFinishLaunchingWithOptions")
        printState()
        return true
    }
    
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
        let token = tokenParts.joined()
        //print("Device Token: \(token)")
    }
    
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Fail to register token \(error.localizedDescription)")
    }
    
    func applicationWillEnterForeground(_ application: UIApplication) {
        print("applicationWillEnterForeground")
        printState()
    }
    
    func applicationDidBecomeActive(_ application: UIApplication) {
        print("applicationDidBecomeActive")
        printState()
    }
    
    func applicationWillResignActive(_ application: UIApplication) {
        print("applicationWillResignActive")
        printState()
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        print("applicationDidEnterBackground")
        printState()
    }
    
    private func printState() {
        switch UIApplication.shared.applicationState {
        case .active:
            print("state: active")
        case .background:
            print("state: background")
        case .inactive:
            print("state: inactive")
        }
    }
}

 

1. App Launch

  • inactive -> active

 

2. Foreground -> Background

  • active -> inactive -> background

 

3. Background -> Foreground

  • (suspended) -> background -> inactive -> active

 

4. Go to App switcher

  • active -> inactive

 

5. User exit the app by app switcher

 

  • active -> inactive -> background -> not running
728x90

댓글