AppDelegate

J.Nomaยท2022๋…„ 2์›” 17์ผ
1

iOS : View : UIKit

๋ชฉ๋ก ๋ณด๊ธฐ
14/17
post-custom-banner

Reference


๐ŸŒ€ AppDelegate

๐Ÿ”ธ ์—ญํ• 

๐Ÿ”˜ ์‹ค์งˆ์ ์ธ root ๊ฐ์ฒด / ์‹œ์Šคํ…œ๊ณผ์˜ ์ƒํ˜ธ์ž‘์šฉ
App delegate ๊ฐ์ฒด๋Š” App ์ „์ฒด๊ฐ€ ๊ณต์œ ํ•˜๋Š” ํ–‰๋™๋“ค์„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. app delegate๋Š” App์˜ ์‹ค์งˆ์ ์ธ root ๊ฐ์ฒด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. UIApplication ๊ฐ์ฒด์™€ ํ•จ๊ป˜ ๋™์ž‘ํ•˜์—ฌ ์‹œ์Šคํ…œ๊ณผ์˜ ์ƒํ˜ธ์ž‘์šฉ์„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. UIApplication ๊ฐ์ฒด์ฒ˜๋Ÿผ, App ๋Ÿฐ์น˜ ์‚ฌ์ดํด ์ดˆ๋ฐ˜์— UIKit์ด ์ด app delegate๋„ ์ƒ์„ฑํ•˜๋ฏ€๋กœ, App์ด ์ผœ์ ธ์žˆ์œผ๋ฉด ํ•ญ์ƒ ์‚ด์•„์žˆ๋‹ค๊ณ  ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค

๐Ÿ”˜ AppDelegate๊ฐ€ ์ฒ˜๋ฆฌํ•˜๋Š” ์ž‘์—…๋“ค

  • App์˜ ์ค‘์‹ฌ์ด ๋˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ์ดˆ๊ธฐํ™”
  • Scene ์„ค์ •/ํ•ด์ œ
  • App ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋˜๋Š” ์•Œ๋ฆผ์— ๋Œ€์‘ (ex. ๋ฉ”๋ชจ๋ฆฌ๋ถ€์กฑ, ๋ฐฑ๊ทธ๋ผ์šด๋“œ ๋‹ค์šด๋กœ๋“œ์™„๋ฃŒ ๋“ฑ)
  • ํƒ€๊ฒŸ์ด ํŠน์ • scene/view/viewController๊ฐ€ ์•„๋‹Œ App ์ž์ฒด์ธ ์ด๋ฒคํŠธ์— ๋Œ€์‘
  • ๋Ÿฐ์น˜ ์‹œ์ ์— ํ•„์š”ํ•œ ์„œ๋น„์Šค ๋“ฑ๋ก (ex. Apple Push Notification ์„œ๋น„์Šค)
  • App ์ƒํƒœ ๋ณต๊ตฌ(State Restoration) ๊ด€๋ฆฌ

๐Ÿ”˜ iOS 12๊นŒ์ง€๋Š” Life-cycle ๊ด€๋ฆฌ๋„ ํ–ˆ์—ˆ๋‹ค
iOS 13๋ถ€ํ„ฐ๋Š” scene ์ƒ๊ธฐ๋ฉด์„œ ์•ˆํ•จ

๐Ÿ”ธ UIApplication

AppDelegate๋Š” UIApplicationDelegate๋ฅผ ์ฑ„ํƒํ•˜๋Š” class์ž…๋‹ˆ๋‹ค. UIApplication๊ณผ UIApplicationDelegate์— ๋Œ€ํ•ด ์•Œ์•„๋ด…์‹œ๋‹ค

๐Ÿ”˜ UIApplicationMain()์— ์˜ํ•ด ์ƒ์„ฑ
๋ชจ๋“  iOS App์€ ๋‹จ ํ•˜๋‚˜์˜ UIApplication ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค (๋งค์šฐ ๋“œ๋ฌผ๊ฒŒ subclass๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค). App์ด ๋Ÿฐ์น˜๋  ๋•Œ, ์‹œ์Šคํ…œ์€ UIApplicationMain()๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” UIApplication ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ UIApplication.shared๋ผ๋Š” ํƒ€์ž… ํ”„๋กœํผํ‹ฐ์— ๊ฝ‚์•„์ค๋‹ˆ๋‹ค

๐Ÿ”˜ ์ด๋ฒคํŠธ ์‚ฌ์ดํด ์…‹์—…
UIApplication ๊ฐ์ฒด๋Š” ์œ ์ € ์ด๋ฒคํŠธ๊ฐ€ ์ฒ˜์Œ์— ์–ด๋–ค ๊ฒฝ๋กœ๋กœ ๊ฐ€์•ผํ• ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ์ฒด๋Š” UIControl ์ธ์Šคํ„ด์Šค์— ์˜ํ•ด ๋ณด๋‚ด์ง„ action ๋ฉ”์‹œ์ง€๋ฅผ ์ ์ ˆํ•œ target ๊ฐ์ฒด์—๊ฒŒ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  App์˜ ๋ชจ๋“  UIView ๊ฐ์ฒด๋ฅผ ์ฐพ๋Š”๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์—ด๋ ค์žˆ๋Š” UIWindow ๊ฐ์ฒด์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค

๐Ÿ”˜ Delegate
UIApplication ํด๋ž˜์Šค๋Š” UIApplicationDelegate ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๋Š” delegate๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ค‘์š”ํ•œ ๋Ÿฐํƒ€์ž„ ์ด๋ฒคํŠธ(App๋Ÿฐ์น˜, ๋ฉ”๋ชจ๋ฆฌ๋ถ€์กฑ, App์ข…๋ฃŒ ๋“ฑ)๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด UIApplication์ด ์ด delegate์—๊ฒŒ ์•Œ๋ ค ์ ์ ˆํžˆ ๋Œ€์‘ํ•˜๋„๋ก ์‹œํ‚ต๋‹ˆ๋‹ค

์ดํ›„ ์ƒ๋žต...


๐ŸŒ€ ์ฃผ์š” ๋ฉ”์„œ๋“œ

๐Ÿ”ธ main()

UIKit App์˜ top-level entry ํฌ์ธํŠธ. App์ด ๋Ÿฐ์น˜๋˜๋ฉด ์‹œ์Šคํ…œ์ด ์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค

๐Ÿ”ธ App Life Cycle ๊ด€๋ จ

๐Ÿ”˜ ๋Ÿฐ์น˜ ์™„๋ฃŒ ์ง์ „
application(didFinishLaunchingWithOptions:)
App์˜ ๋Ÿฐ์น˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๊ฑฐ์˜ ์™„๋ฃŒ๋˜์–ด ์‹คํ–‰๋˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ๋˜๋Š” ์ฝœ๋ฐฑ

๐Ÿ”˜ Active ์ง„์ž…ํ•จ
applicationDidBecomeActive()
App์ด active ๋˜์—ˆ์„ ๋•Œ

๐Ÿ”˜ Resign ์ง์ „
applicationWillResignActive()
App์ด ํฌ์ปค์Šค๋ฅผ ์žƒ๋Š” ๊ฒฝ์šฐ (ex. ํ™ˆ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด ๊ฐ€์žฅ ๋จผ์ € ํ˜ธ์ถœ๋จ)

๐Ÿ”˜ Background ์ง„์ž…ํ•จ
applicationDidEnterBackground()
๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ง„์ž…๋จ

๐Ÿ”˜ Foreground ์ง์ „
applicationWillEnterForeground()
๋‹ค์‹œ ํฌ์–ด๊ทธ๋ผ์šด๋“œ๋กœ ๋‚˜์˜ค๊ธฐ ์ง์ „

๐Ÿ”ธ Scene ์ƒ์„ฑ/์‚ญ์ œ ๊ด€๋ จ

๐Ÿ”˜ Scene ์ƒ์„ฑ
application(configurationForConnecting:)
์ƒˆ๋กœ์šด Scene์„ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉํ•  configuration ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”์„œ๋“œ. UIKit์€ ์ƒˆ๋กœ์šด scene์„ ์ƒ์„ฑํ•˜๊ธฐ ์ง์ „์— ์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค

scene-configuration ๋ฐ์ดํ„ฐ ์ด ๋ฉ”์„œ๋“œ์— ๊ตฌํ˜„ํ•˜์—ฌ ์ œ๊ณตํ•˜๊ฑฐ๋‚˜, Info.plist์— ํฌํ•จ์‹œ์ผœ ์ œ๊ณตํ•  ์ˆ˜๋„ ์žˆ๋‹ค. Info.plist์— ํฌํ•จ์‹œํ‚ค์ง€ ์•Š์•˜๋‹ค๋ฉด ์ด ๋ฉ”์„œ๋“œ๋ฅผ ๋ฐ˜๋“œ์‹œ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. Info.plist์— ํฌํ•จ์‹œ์ผฐ๋”๋ผ๋„ scene-configuration์„ ๋™์ ์œผ๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค

๐Ÿ”˜ Scene ์‚ญ์ œ
application(didDiscardSceneSessions:)
App switcher์—์„œ ํ•ด๋‹น App์˜ ํ•˜๋‚˜ ์ด์ƒ์˜ scene์„ ๋‚ ๋ฆฌ๋ฉด ํ˜ธ์ถœ๋œ๋‹ค. scene ์‚ญ์ œ์— ๋”ฐ๋ผ App ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ฑฐ๋‚˜ scene์ด ๊ฐ–๊ณ ์žˆ๋˜ ๋ฆฌ์†Œ์Šค๋ฅผ ํ•ด์ œํ•˜๋Š” ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค

App์ด not running์ด๋ผ๋ฉด ๋‹ค์Œ ๋Ÿฐ์น˜ ์‹œ์ ์— ํ˜ธ์ถœ๋œ๋‹ค

์‹œ์Šคํ…œ์ด ๋ฉ”๋ชจ๋ฆฌ ๋ฌธ์ œ๋กœ scene์„ disconnectํ•  ๋•Œ๋Š” ํ˜ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค. ์ด ๋•Œ๋Š” scene์€ ์‚ฌ๋ผ์ง€์ง€๋งŒ ๊ทธ ๊ด€๋ จ session๋“ค์€ ๋ณด์กด๋œ๋‹ค

๐Ÿ”ธ ๊ทธ ์™ธ ์ค‘์š”ํ•œ Event๋“ค

๐Ÿ”˜ Memory Warnings
applicationDidReceiveMemoryWarning(_:)
App ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ๋„ˆ๋ฌด ๋†’์„ ๋•Œ ์ˆ˜์‹ ๋˜๋ฉฐ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ค„์ž…๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ Responding to Memory Warnings ์ฐธ๊ณ 

๐Ÿ”˜ Data Protection
applicationProtectedDataDidBecomeAvailable(_:)
applicationProtectedDataWillBecomeUnavailable(_:)
์œ ์ €๊ฐ€ device๋ฅผ lock/unlockํ•  ๋•Œ ์ˆ˜์‹ ๋ฉ๋‹ˆ๋‹ค

๐Ÿ”˜ Handoff task
application(_:didUpdate:)
NSUserActivity ๊ฐ์ฒด๊ฐ€ ์ฒ˜๋ฆฌ๋  ํ•„์š”๊ฐ€ ์žˆ์„ ๋•Œ ์ˆ˜์‹ ๋ฉ๋‹ˆ๋‹ค

* handoff : ์„œ๋กœ ๋‹ค๋ฅธ ์• ํ”Œ ๊ธฐ๊ธฐ์—์„œ ๊ธฐ์กด ์ž‘์—…์„ ๊ณ„์†ํ•˜๋Š” ๊ฒƒ

๐Ÿ”˜ Time changes
applicationSignificantTimeChange(_:)
(ํ†ต์‹ ์‚ฌ๊ฐ€ ์‹œ๊ฐ„ update๋ฅผ ๋ณด๋‚ธ๋‹ค๋˜์ง€) ์—ฌ๋Ÿฌ ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ์‹œ๊ฐ„์ด ๋ณ€๊ฒฝ๋  ๋•Œ ์ˆ˜์‹ ๋ฉ๋‹ˆ๋‹ค

๐Ÿ”˜ Open URLs
application(_:open:options:)
App์ด ๋ฆฌ์†Œ์Šค๋ฅผ ์˜คํ”ˆํ•  ํ•„์š”๊ฐ€ ์žˆ์„ ๋•Œ ์ˆ˜์‹ ๋ฉ๋‹ˆ๋‹ค

profile
๋…ธ์…˜์œผ๋กœ ์ด์‚ฌ๊ฐ‘๋‹ˆ๋‹ค https://tungsten-run-778.notion.site/Study-Archive-98e51c3793684d428070695d5722d1fe
post-custom-banner

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 8์›” 1์ผ

์•ˆ๋…•ํ•˜์„ธ์š” ๋„ˆ๋ฌด ์ข‹์€ ๊ธ€ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
ํ˜น์‹œ ์Šคํ„ฐ๋”” ๋…ธ์…˜์— ์ •๋ฆฌํ•˜๊ณ  ๊ณต์œ ๋ฅผ ํ•ด๋„ ๋ ๊นŒ์š” ? ์ถœ์ฒ˜๋Š” ํ•ด๋‹น ๋งํฌ๋กœ ๋ฐํžˆ๊ฒ ์Šต๋‹ˆ๋‹ค.

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ