본문 바로가기

IOS Swift

앱이 In - active 상태가 되는 시나리오 - Swift

 

 

Not running 앱이 실행되지 않았거나 완전히 종료되었을 때 상태입니다.
In-active(비활성화) 앱이 실행되면서 포어그라운드에 진입하지만 어떠한 이벤트도 받지 않는 상태입니다.
Active(활성화)  앱이 실행 중이며 포어그라운드에 있고 이벤트를 받고 있는 상태입니다.
Background 앱이 백그라운드에 있으며 다른 앱으로 전환되었거나 홈버튼을 눌러 밖으로 나갔을 때의 상태입니다.
Suspended              :  백그라운드에서 특별한 작업이 없을 경우 전환되는 상태입니다.

 

시나리오 

  • 다른 상태로 넘어가기 전에 앱은 반드시 이 상태를 거침
  • 전화나 메시지 같은 interrupt 발생 시
  • 미리알림 같은 특정 알림창이 화면을 덮어서 앱이 실질적으로 event를 받지 못하는 상태 등이 여기에 해당

 

실험 방법 >  SceneDelegate에서 UI State 변경 시 불리는 메소드들로 UI State를 print()로 찍어봤다.

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
    var window: UIWindow?
 
    // MARK: - UIWindowSceneDelegate
    
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        DispatchQueue.global().async {
            
            for i in 0..<10 {
        print(scene.activationState.rawValue, "start")
            }
        }
        if let userActivity = connectionOptions.userActivities.first ?? session.stateRestorationActivity {
            if !configure(window: window, with: userActivity) {
                Swift.debugPrint("Failed to restore from \(userActivity)")
            }
        }
        // The `window` property will automatically be loaded with the storyboard's initial view controller.
    }
    
    func stateRestorationActivity(for scene: UIScene) -> NSUserActivity? {
        return scene.userActivity
    }
    
    // Utilities
    
    func configure(window: UIWindow?, with activity: NSUserActivity) -> Bool {
        var configured = false

        if activity.title == Photo.GalleryOpenDetailPath {
            if let photoID = activity.userInfo?[Photo.GalleryOpenDetailPhotoIdKey] as? String {
                // Restore the view controller with the photoID.
                if let photoDetailViewController = PhotoDetailViewController.loadFromStoryboard() {
                    photoDetailViewController.photo = Photo(name: photoID)
                    
                    if let navigationController = window?.rootViewController as? UINavigationController {
                        navigationController.pushViewController(photoDetailViewController, animated: false)
                        configured = true
                    }
                }
            }
        }
        return configured
    }
    
    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).
        print(scene.activationState.rawValue, "sceneDidDisconnect")
    }

    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.
        DispatchQueue.global().async {
            for i in 0..<10 {
        print(scene.activationState.rawValue, "sceneDidBecomeActive")
            }
        }
    }

    
    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).

        DispatchQueue.global().async {
            for i in 0..<10 {
        print(scene.activationState.rawValue, "sceneWillResignActive")
            }
        }
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
        // Called as the scene transitions from the background to the foreground.
        // Use this method to undo the changes made on entering the background.
    
        DispatchQueue.global().async {
            for i in 0..<10 {
        print(scene.activationState.rawValue, "sceneWillEnterForeground")
            }
        }
    }

    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.
        print(scene.activationState.rawValue, "sceneDidEnterBackground")
    } 
}

 

1. 앱 처음 켤 때

-1 (not running) -> 1 (In - active) -> 0 (active)

2. 포그라운드 -> 백그라운드

0 (active) -> 1 (In - active) -> 2 (bacground)

 

3. 백그라운드 -> 포그라운드

2 (background) -> 1 (In - active) -> 0 (active)

 

결론 : 다른 상태로 넘어가기 전에 앱은 반드시 이 상태를 거친다. 확인 땅땅

 

반응형

'IOS Swift' 카테고리의 다른 글

비디오 & 오디오 형식 변환을 위한 사전지식  (0) 2021.10.04
멀티윈도우 지원하는 앱 만들기 - iOS  (8) 2021.09.28
SceneDelegate 란?  (2) 2021.09.16
MVC와 MVVM의 차이점 및 장단점  (0) 2021.09.09
Optional 이란?  (2) 2021.09.02