[iOS] AppDelegate

r1verfuture·2022년 4월 16일
0

iOS

목록 보기
25/30

AppDelegate.swift

  • 프로젝트를 만들면 SceneDelegate.swift 파일과 함께 자동으로 생성되는 파일 (위 사진 참고)

  • AppDelegate 클래스 정의 부분이 포함되어 있는 파일 (위 사진 참고)
  • Entry Point (@main 키워드) 와 입력 이벤트를 앱에 전달하는 Run Loop 를 생성한다.
  • 생명 주기를 관리한다.
  • UIResponder 클래스를 상속받고, UIApplicationDelegate 프로토콜을 채택한다.
  • iOS 13 이후부터는 'SceneDelegate' 이라는 개념이 생겨서 AppDelegate 에서 했던 일 중 일부를 대신한다. (SceneDelegate.swift 파일을 보면 'UIWindowSceneDelegate' 프로토콜을 채택하고 있는 것을 볼 수 있는데 'UIWindowSceneDelegate' 프로토콜은 'UISceneDelegate' 프로토콜을 채택하고 있고, 'UISceneDelegate' 프로토콜 안에 있는 몇가지 메소드들이 기존에 'AppDelegate' 가 담당하고 있던 일들을 대신하고 있는 것을 볼 수 있다.)

UIResponder

  • 이벤트에 응답하고 처리하기 위한 추상적인 인터페이스
  • Apple 공식 문서

UIApplicationDelegate

  • 프로토콜 중 하나
  • 앱에서 공유된 행동들을 관리하는 메소드들 (아래 사진 참고) 의 집합
  • Apple 공식 문서

UISceneDelegate

  • 프로토콜 중 하나
  • Scene 내에서 발생하는 생명 주기 이벤트에 응답하는데 사용하는 핵심 메소드들
  • Apple 공식 문서

  • 위 사진을 보면 UISceneDelegate 정의 부분 윗쪽에 @available(iOS 13.0, *) 라고 되어 있는 것을 볼 수 있는데, 'SceneDelegate' 라는 개념이 iOS 13 버전 이후부터 나온 것이라는 증거가 된다.
  • 앱 사용하던 중 홈 화면으로 이동할 때 : applicationWillResignActive -> applicationDidEnterBackground 순으로 호출된다.
  • 홈 화면에서 앱을 다시 켤 때 (Background 상태였던 앱을 켤 때) : applicationWillEnterForeground -> applicationDidBecomeActive 순으로 호출된다.
  • 홈 화면에서 앱을 다시 켤 때 (아예 종료된 앱을 켤 때) : applicationDidBecomeActive 만 호출된다.
  • 앱을 사용하던 중에 알림 센터로 이동했을 때 : applicationWillResignActive -> applicationDidBecomeActive -> applicationWillResignActive 순으로 호출된다.
  • 앱을 사용하던 중에 제어 센터로 이동할 때 : applicationWillResignActive 가 호출된다.
  • 제어 센터에서 다시 앱으로 돌아갈 때 : applicationDidBecomeActive 가 호출된다.
  • 앱을 사용하던 중에 홈 버튼을 눌러 앱을 뒤로 보낸 다음 앱을 종료할 때 : applicationWillResignActive -> applicationDidEnterBackground -> applicationWillTerminate 순으로 호출된다.

UISceneDelegate 가 대신하게 된 UIApplicationDelegate 메소드들

UIApplicationDelegateUISceneDelegate
applicationWillEnterForeground(_ application: UIApplication)sceneWillEnterForeground(_ scene: UIScene)
applicationDidEnterBackground(_ application: UIApplication)sceneDidEnterBackground(_ scene: UIScene)
applicationWillResignActive(_ application: UIApplication)sceneWillResignActive(_ scene: UIScene)
applicationDidBecomeActive(_ application: UIApplication)sceneDidBecomeActive(_ scene: UIScene)
applicationWillEnterForeground(_ application: UIApplication)
  • 앱이 Foreground 상태가 되었을 때 호출된다. (Background -> Foreground)
  • UIKit 은 이 메소드를 호출하기 전에 'willEnterForegroundNotification' 이라는 Notification 을 post 해서 앱 전체에서 해당 이벤트를 처리할 수 있도록 해준다.
applicationDidEnterBackground(_ application: UIApplication)
  • 앱이 Background 상태가 되었을 때 호출된다. (In-Active -> Background)
  • Background 상태가 되면 타이머를 무효화하고, 앱 상태 정보를 저장하게 된다.
  • 이 메소드는 빠르게 반환되기 때문에 만약 이 메소드에서 수행할 작업이 있고, 추가적인 실행 시간이 필요하다면 beginBackgroundTask(expirationHandler:) 메소드를 사용하면 된다.
  • UIKit 은 이 메소드를 호출한 뒤 'didEnterBackgroundNotification' 이라는 Notification 을 post 해서 앱 전체에서 해당 이벤트를 처리할 수 있도록 해준다.
applicationWillResignActive(_ application: UIApplication)
  • 앱이 Active 상태를 포기했을 때 호출된다. (Active -> In-Active)
  • 앱 실행하던 중에 홈 화면으로 나가거나 제어 센터, 알림 센터를 사용할 때 In-Active 상태가 되기 때문에 이때 호출된다.
  • UIKit 은 이 메소드를 호출한 뒤 'willResignActiveNotification' 이라는 Notification 을 post 해서 앱 전체에서 해당 이벤트를 처리할 수 있도록 해준다.
applicationDidBecomeActive(_ application: UIApplication)
  • 앱이 Active 상태가 되었을 때 호출된다. (In-Active -> Active)
  • UIKit 은 이 메소드가 호출되면 'didBecomeActiveNotification' 이라는 Notification 을 post 해서 앱 전체에서 해당 이벤트를 처리할 수 있도록 해준다.
applicationWillTerminate(_ application: UIApplication)
  • SceneDelegate 에 없지만 위 메소드들처럼 앱 실행 상태와 관련된 메소드이다.
  • 앱이 종료되기 직전에 호출된다.
  • 이 메소드가 호출된 후 앱이 종료되면 메모리에서 완전히 제거된다.
  • UIKit 은 이 메소드를 호출한 뒤 'willTerminateNotification' 이라는 Notification 을 post 해서 앱 전체에서 해당 이벤트를 처리할 수 있도록 해준다.

참고

profile
#iOS #Swift #Developer #Python

0개의 댓글

관련 채용 정보