๐ŸŽ ์•„์ด-์˜ค-์—์Šค/๐Ÿ“ฑ iOS ๊ฐœ๋ฐœ

[iOS] Scene Life Cycle: iOS 13 ๋ถ€ํ„ฐ ์‹œ์ž‘๋œ Scene-Based Event

๊ฐœ๋ฐœํ•˜๋Š” ํ›ˆ์ด 2021. 9. 19. 19:20

Scene ์ด ๋ญ”๊ฐ€์—ฌ..

https://developer.apple.com/documentation/uikit/app_and_environment/scenes

iOS 13 ๋ถ€ํ„ฐ๋Š” ํ•˜๋‚˜์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ฐฝ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์•„์ดํŒจ๋“œ๋ฅผ ์ƒ๊ฐํ•˜์‹œ๋ฉด ํŽธํ•˜์‹คํ…๋ฐ์š”, ํ•˜๋‚˜์˜ ์•ฑ์„ ๋™์‹œ์— ์—ฌ๋Ÿฌ๊ฐœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์•ฑ์ด ๊ฐ€์ง€๋Š” ์—ฌ๋Ÿฌ ์ฐฝ๋“ค์„ ์ง€์นญํ•˜๋Š” ์šฉ์–ด๋กœ Scene์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ฐ Scene ๋“ค์€ ๊ฐ์ž์˜ ViewController ์™€ UI ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์•ฑ์ด ์‹คํ–‰๋  ๋•Œ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•˜๋‚˜์˜ Scene์ด ์‹คํ–‰๋˜๊ณ  ๋‹ค์ค‘ Scene์€ iPad์—์„œ๋งŒ ์ง€์›๋ฉ๋‹ˆ๋‹ค!

 

Scene Life Cycle 

Scene ๋“ค์€ ์‚ฌ์šฉ์ž์˜ ์‚ฌ์šฉ์— ๋”ฐ๋ผ ์ƒํƒœ๊ฐ€ ๋ณ€ํ™”ํ•ฉ๋‹ˆ๋‹ค. 

https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle

์• ํ”Œ ๊ณต์‹๋ฌธ์„œ์—์„œ ์†Œ๊ฐœํ•˜๋Š” ๊ทธ๋ฆผ์„ ๋”ฐ๋ผ๊ฐ€๋ณด๋ฉด ์ข€ ๋” ์ดํ•ด๊ฐ€ ์‰ฝ๋„ค์š”! ๊ฐ ์ƒํƒœ์— ๋Œ€ํ•œ ์„ค๋ช…๋„ ๊ณต์‹๋ฌธ์„œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋จผ์ €, UIKit ์ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ Scene์— ์—ฐ๊ฒฐ๋˜๋ฉด ์•ฑ์„ ์‹œ์ž‘ํ•  ๋•Œ ํ‘œ์‹œํ•  UI์™€ Scene์„ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ๋“ค์„ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ ์—์„œ ์ •๋ฆฌํ–ˆ๋˜๋Œ€๋กœ, Scene์€ ํ•œ Scene ์„ธ์…˜์— ๋Œ€ํ•œ Configuration์ด AppDelegate์—์„œ ์„ค์ •๋˜๊ณ , ์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ SceneDelegate์˜ willConnectToSession์—์„œ UI๋ฅผ ๋งŒ๋“ค์–ด๋ƒ…๋‹ˆ๋‹ค.

 

์ค€๋น„๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด UIKit์€ Scene ์„ Unattatched ์ƒํƒœ๋กœ ๋งŒ๋“ค๊ณ  ์‚ฌ์šฉ์ž๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ™”๋ฉด์ธ Foreground ๋กœ Scene์„ ์ด๋™์‹œํ‚ต๋‹ˆ๋‹ค.

 

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹œ์ž‘๋˜๋ฉด์„œ Foreground๋กœ ๋“ค์–ด์˜ค๋ฉด, ์ด์ œ ์ด Scene ๊ณง๋ฐ”๋กœ ์‚ฌ์šฉ์ž์™€ ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. Scene์€ ๋น ๋ฅด๊ฒŒ Foreground Inactive ์ƒํƒœ๋ฅผ ๊ฑฐ์ณ Foreground Active ์ƒํƒœ๋กœ ์ „ํ™˜๋ฉ๋‹ˆ๋‹ค. Foreground Active ์ƒํƒœ๋Š” ํ•ญ์ƒ Forground Inactive ์ƒํƒœ๊ฐ€ ์„ ํ–‰๋ฉ๋‹ˆ๋‹ค!

 

์‚ฌ์šฉ์ž๊ฐ€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” Foreground Active์™€ Foreground Inactive ์ƒํƒœ๋ฅผ ๋ฐ˜๋ณตํ•˜๊ฒ ์ฃ ? Foreground Inactive๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด Foreground์— ์žˆ์ง€๋งŒ ์ „ํ™”, ๋ฌธ์ž, ์•Œ๋ฆผ ๋“ฑ์œผ๋กœ ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์—†๋Š” ์ƒํ™ฉ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

์‚ฌ์šฉ์ž๊ฐ€ ์•ฑ ์Šค์œ„์ณ๋ฅผ ํ†ตํ•ด ๋‹ค๋ฅธ ์•ฑ์œผ๋กœ ์ „ํ™˜ํ•˜๊ฑฐ๋‚˜, ํ™ˆ์œผ๋กœ ์ด๋™ํ•˜๋ฉด ๋” ์ด์ƒ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด Forground์— ์žˆ์ง€ ์•Š๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ๋Š” Scene์˜ ์ƒํƒœ๊ฐ€ Background ๋กœ ์ „ํ™˜๋ฉ๋‹ˆ๋‹ค. Background ์ƒํƒœ๋กœ ์ง„์ž…ํ•˜๋ฉด Scene์ด ์‚ฌ์šฉํ•˜๋˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ตœ๋Œ€ํ•œ ํ•ด์ œํ•ฉ๋‹ˆ๋‹ค.

 

์ผ์ •์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด Background์— ์žˆ๋˜ Scene์€ ์ž๋™์œผ๋กœ Disconnect ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Scene์˜ ์ƒํƒœ๋Š” ์ข…๋ฃŒ์œ ์˜ˆ๋ฅผ ์˜๋ฏธํ•˜๋Š” Suspended ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

 

Background๋‚˜ Suspended ์ƒํƒœ๊ฐ€ ๋œ Scene์€ UIKit์— ์˜ํ•ด ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํšŒ์ˆ˜๋œ ๋’ค์— Unattatched ์ƒํƒœ๋กœ ๋‹ค์‹œ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ Scene๊ณผ ๊ด€๋ จ๋œ ๋ชจ๋“  ๋ฉ”๋ชจ๋ฆฌ๋Š” ์ด ์‹œ์ ์—์„œ ํ•ด์ œ๋ฉ๋‹ˆ๋‹ค. ์ƒํƒœ๋ฅผ ๋ณต์›ํ•˜๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์€ ์—ญ์‹œ ์—ฌ๊ธฐ ์— ์ •๋ฆฌํ•ด๋‘์—ˆ์Šต๋‹ˆ๋‹ค.

์ง์ ‘ ํ™•์ธํ•ด๋ณด์ž 

์—ญ์‹œ ๋ˆˆ์œผ๋กœ ์ง์ ‘ ๋ณด์ง€ ์•Š์œผ๋ฉด ๋ฏฟ์„ ์ˆ˜ ์—†์ฃ ..

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        print("scene will connect")
        guard let _ = (scene as? UIWindowScene) else { return }
    }

    func sceneDidDisconnect(_ scene: UIScene) {
        print("scene did disconnect")
    }

    func sceneDidBecomeActive(_ scene: UIScene) {
        print("scene did become active")
    }

    func sceneWillResignActive(_ scene: UIScene) {
        print("scene will resign active")
    }


    func sceneWillEnterForeground(_ scene: UIScene) {
        print("scene will enter foreground")
    }
    
    func sceneDidEnterBackground(_ scene: UIScene) {
        print("scene did enter background")
    }


}

SceneDelegate.swift ํŒŒ์ผ์„ ์—ด์–ด์„œ ๊ฐ ์ƒํƒœ์— ๋“ค์–ด๊ฐ€๋ฉด ์ถœ๋ ฅ์„ ํ•˜๋„๋ก ํ•ด๋‘์—ˆ์Šต๋‹ˆ๋‹ค.

 

์ด์ œ ์‹คํ–‰ํ•ด๋ณผ๊นŒ์š”?

์•ฑ์„ ์‹คํ–‰ํ•˜๋ฉด ๊ณง๋ฐ”๋กœ Connect -> (Unattatched) ->  Foreground(Inactve) -> Active ๊ฐ€ ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

์—ฌ๊ธฐ์„œ Inactive ์ƒํƒœ๋กœ ์ „ํ™˜๋˜๋Š” ์ˆœ๊ฐ„์„ ํ™•์ธํ•ด๋ณด๊ธฐ ์œ„ํ•ด์„œ ์‹œ๋ฆฌ๋ฅผ ํ˜ธ์ถœํ•ด๋ณผ๊ฒŒ์š”.

์ด๋ ‡๊ฒŒ ์‹œ๋ฆฌ๊ฐ€ ๋ถˆ๋ฆฌ๋Š” ์ˆœ๊ฐ„ Inactive ์ƒํƒœ๊ฐ€ ๋˜์—ˆ๋‹ค๊ฐ€ ์‹œ๋ฆฌ๊ฐ€ ๊บผ์ง€๋ฉด Active ์ƒํƒœ๊ฐ€ ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

์ž ๊ทธ๋Ÿผ ์ด์ œ ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ์ด๋™ํ•ด๋ณด์ฃ .

 

์ด๋ ‡๊ฒŒ ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ๊ฐˆ ๋•Œ๋„ Inactive๋ฅผ ํ•œ๋ฒˆ ๊ฑฐ์ณ์„œ Background ์ƒํƒœ๋กœ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.

 

Scene์˜ ์ƒํƒœ๋“ค์€ ์ด๋ฆ„๋„ ์ง๊ด€์ ์ด๊ณ  SceneDelegate๋กœ ์‰ฝ๊ฒŒ ํ™•์ธํ•ด๋ณผ ์ˆ˜ ์žˆ์–ด์„œ ์ดํ•ดํ•˜๊ธฐ ํŽธํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ๊ณง iOS 12 ๊นŒ์ง€ ์‚ฌ์šฉํ•˜๋˜ App์˜ ์ƒ๋ช…์ฃผ๊ธฐ์™€ ViewController์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋„ ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! ์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)