bannaviiOS) appdelegate와 scenedelegate(UILifeCycle에 관하여)

2022. 11. 9. 17:42iOS/iOS

728x90
반응형

앱은 background, foreground에 있는가에 따라서 시스템 알림에 응답하고 다른 중요한 시스템 관련된 이벤트를 처리하게 됩니다.

 

appdelegate에서 foreground ↔︎ background 전환에 대해 공부하던 중, 아래와 세개와 같은 함수를 발견해서 사용해보았지만 함수 호출이 되지 않았습니다. 이에 관련하여 stackoverflow에서 프로젝트의 info.plist에서 아래와 같은 property값을 NO로 설정하는것이 appdelegate에서 해당 함수들을 호출하는 방법이라고 했지만 해결되지 않았습니다.

참고 글)

https://stackoverflow.com/questions/15405442/applicationdidenterbackground-and-applicationwillenterforeground-method-are-not

 

applicationDidEnterBackground and applicationWillEnterForeground method are not called when pressed home button in iOS simulator

I need a long running task to be done in background as well as in foreground. This updates the core data. So to maintain UI responsive I created an another thread where I use different

stackoverflow.com

func applicationWillEnterForeground(_ application: UIApplication) {
        DLog.d("🍞실행된 앱이 background에서 foreground로 이동될 때 실행되는 메서드 아직 foreground에서 실행x")
    }
    
    func applicationDidBecomeActive(_ application: UIApplication) {
        DLog.d("🍞실행된 앱이 foreground에 표시될때 호출되는 메서드")
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        DLog.d("🍞실행된 앱이 background에 진입했을때 호출되는 메서드")
    }

 

 

그런데, scenedelegate에서 아래의 함수들을 구현해서 사용했을때 함수 호출이 되는것을 확인했고, 왜 appdelegate에선 작동이 안되는 함수가 scenedelegate에서는 작동이 되는지에 대해 고민하던중 아래의 글을 발견했습니다.

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.
        DLog.d("🥕실행된 앱이 foreground에 표시될때 호출되는 메서드")
}
    
    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.
        DLog.d("🥕실행된 앱이 background에 진입했을때 호출되는 메서드")
}

참고 글) iOS Application Life Cycle

 

글의 내용인 즉슨

앱의 상태가 바뀔 때 UIKit은 적절한 delegate object의 method를 호출하여 우리에게 알려주는데,

  • iOS 13 이상UISceneDelegate 개체를 사용하여 scene-based app에서 발생하는 life-cycle event에 응답하면 된다.
  • iOS 12 이하life-cycle event에 응답하기 위해서 UIApplicationDelegate 개체를 사용하면 된다.
  • iOS13 이상은 Appdelegate의 역할 중 UI의 상태를 알 수 있는 UILifeCycle에 대한 부분을 SceneDelegate가 대체한다

    라는 내용이었습니다.

실제로, smartstore프로젝트의 Deployment Info가 iOS13버전 이상을 타겟으로 하기 때문에 UILifeCycle에 대한 부분을 appdelegate가 아닌 scenedeleate가 대체했었고, 그래서 appdelegate에서 호출되지 않았었음을 알게되었습니다.

 

 

 

결론)

1. WWDC열심히 보자.

2. 뭐가 어떻게 변화되는지를 알아야 몸이 덜 고생한다.

3. 부장님께 confluence내용 따봉받아서 기분좋음(tmi)

728x90
반응형