본문 바로가기
앱/iOS(Swift)

[iOS] 앱 생명주기

by 리드맥 2022. 1. 23.

13버전 이후 app delegate 역할이 app deletegate, scene delegate로 나누어졌다
-> 아이패드 OS의 새로운 멀티윈도우 지원기능을 사용하기 위해서
ex) 크롬을 왼쪽에 띄워두고 다른어플을 오른쪽에 띄울 수 있게 된것]
AppDelegate: 라이프 사이클 이벤트에 대해 처리
AppDelegate.swift

 func application(_ application: UIApplication, didFinishLaunchingWithOptions
                  launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }
  • 앱이 시작되고 앱의 설정이 완료되면 호출
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)
   
  • 앱에 표시할 새로운 scene나 위도우가 필요할때마다 호출, 앱시작시 호출되지 않고 새로운scene, window를 가져와야 하는경우에만 호출 -> scene session을 추가하기 위한 메소드
 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.
    }
  • scene을 삭제, 프로그래밍 방식으로 삭제될때 호출,

SceneDelegate: AppDelegate로부터 UIWindow 와 관련된 부분을 담당, UI및 데이터를 화면에 표시하는것을 담당
-> UISceneSession의 scene생성, 파괴, 폭원과 같은 scene라이프 사이클 이벤트 담당
SceneDelegate.swift

func scene(_ scene: UIScene, willConnectTo session: UISceneSession,
           options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let _ = (scene as? UIWindowScene) else { return }
    }
  • UISceneSession의 라이프 사이클에서 호출되는 첫번째 메서드, scene이 앱에 추가 될때 호출
func sceneDidDisconnect(_ scene: UIScene) {
        // Called as the scene is being released by the system.
        // This occurs shortly after the scene enters the background, or when its session is discarded.
        // Release any resources associated with this scene that can be re-created the next time the scene connects.
        // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
    }
  • scene이 백그라운드 상태로 전환될때, iOS는 메모리 회수를 위해서 scene를 삭제할수있는데, 앱이 종료되거나 실행되지 않는것을 말하는게 아닌 scene만 session에서 연결 해제되고 비활성화된다는뜻
 func sceneDidBecomeActive(_ scene: UIScene) {
        // Called when the scene has moved from an inactive state to an active state.
        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
    }
  • sceneWillEnterForeground메서드 바로 다음에 호출, scene가 화면에 표시괴도록 준비될때 사용
func sceneWillResignActive(_ scene: UIScene) {
        // Called when the scene will move from an active state to an inactive state.
        // This may occur due to temporary interruptions (ex. an incoming phone call).
    }
  • 사용자가 scene과의 상요작용을 중지할때 호출
func sceneDidEnterBackground(_ scene: UIScene) {
        // Called as the scene transitions from the foreground to the background.
        // Use this method to save data, release shared resources, and store enough scene-specific state information
        // to restore the scene back to its current state.
    }
  • scene이 화면에 표시되지않고 백그라운드로 진입할때 호출


13버전이후앱 라이프사이클

' > iOS(Swift)' 카테고리의 다른 글

[Swift] Tuples, Range  (0) 2022.01.27
[iOS] MVC 패턴  (0) 2022.01.26
[Swift] Type Casting  (0) 2022.01.23
[Swift] for in, forEach 차이  (0) 2022.01.23
[Swift] DeadLock  (0) 2022.01.23

댓글