적당한 고통은 희열이다

- 댄 브라운 '다빈치 코드' 중에서

Swift iOS 앱 개발/Swift

[Swift iOS] How to detect app entering foreground / background

hongssup_ 2021. 6. 16. 18:22
반응형

앱이 백그라운드에서 실행되고 있는지를 체크하여 다시 활성화 되었을 때만 실행하고 싶은 코드가 있어 이를 체크해주는 메서드를 찾아보았다. 

다음 메서드들이 바로 AppDelegate에서 제공되는 앱의 실행 상태를 감지해주는 메서드들이다. 

func applicationDidEnterBackground(_ application: UIApplication) {
    print("applicationDidEnterBackground")
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
    
func applicationWillEnterForeground(_ application: UIApplication) {
    print("applicationWillEnterForeground")
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
    
func applicationDidBecomeActive(_ application: UIApplication) {
    print("applicationDidBecomeActive")
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    //KOSession.handleDidBecomeActive()
}
    
func applicationWillTerminate(_ application: UIApplication) {
    print("applicationWillTerminate")
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

비활성 -> 활성

applicationWillEnterForeground(_:)

: Tells the delegate that the app is about to enter the foreground. 

앱이 처음 켜질 때는 실행되지 않는다. 백그라운드에 있다가 다시 넘어오면서 활성화될 때 호출되는 메서드. 

applicationDidBecomeActive(_:)

: Tells the delegate that the app has become active. 

앱이 처음 켜질 때도, 다시 활성화될 때도 호출되는 메서드. 

 

 

참고 : 어플이 중단될 때, 다시 실행될 때 감지하는 법

728x90
반응형