Skip to content

Latest commit

ย 

History

History
90 lines (65 loc) ยท 6.01 KB

UIApplicationMain.md

File metadata and controls

90 lines (65 loc) ยท 6.01 KB

์•ฑ์ด ์‹œ์ž‘ํ•  ๋•Œ main.c ์— ์žˆ๋Š” UIApplicationMain ํ•จ์ˆ˜์— ์˜ํ•ด์„œ ์ƒ์„ฑ๋˜๋Š” ๊ฐ์ฒด๋Š” ๋ฌด์—‡์ธ๊ฐ€?

์ฐธ๊ณ ํ•œ ์ข‹์€ ๊ธ€

๊ฐ™์ด ๋ณด๋ฉด ์ข‹์€ ๋‹ต๋ณ€

๋‹ต๋ณ€

UIApplicationMain(_: _: _: _:)ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด UIApplication ์‹ฑ๊ธ€ํ„ด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.

Obj-C์—์„œ๋Š” main.c ์—์„œ UIApplicationMain(_: _: _: _:) ๋ฅผ ํ˜ธ์ถœํ–ˆ์ง€๋งŒ, Swift์—์„œ๋Š” AppDelegate.swift์—์„œ AppDelegateํด๋ž˜์Šค ์•ž์— ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ๋ถ™์€ @UIApplicationMain๋ฅผ ํ†ตํ•ด UIApplicationMain(_: _: _: _:) ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

์ดํ•˜ ์ง์—ญ

Declaration

@MainActor class UIApplication : UIResponder
---
@available(iOS 2.0, *)
open class UIApplication: UIResponder {
    open class var shared: UIApplication {get}
}

UIResponder?

OverView

UIApplication? : iOS์—์„œ ๋™์ž‘์ค‘์ธ ์•ฑ๋“ค์˜ ์ง‘์ค‘ ์ œ์–ด ๋ฐ ์กฐ์ • ์ง€์ ์ž…๋‹ˆ๋‹ค.

๊ทธ๋งŒํผ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•œ๋‹ค๋Š”๊ฑฐ๊ฒ ์ฃ ?


๋ชจ๋“  iOS ์•ฑ์—์„œ UIApplication์€ ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค(Singleton(์‹ฑ๊ธ€ํ†ค))๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค(์ดํ•˜ application ๊ฐ์ฒด). ์•ฑ์ด ๋™์ž‘ํ•  ๋•Œ ์‹œ์Šคํ…œ์€ UIApplicationMain(_: _: _: _:) ์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, ์ด ํ•จ์ˆ˜๊ฐ€ ๋ฐ”๋กœ shared๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ UIApplication์˜ ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

[์ •๋ฆฌ]
์•ฑ์ด ์‹œ์ž‘๋˜๋ฉด ์‹œ์Šคํ…œ์ด UIApplicationMain(_: _: _: _:) ๋ฅผ ํ˜ธ์ถœํ•ด์„œ UIApplication์˜ ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด UIApplicationMain(_: _: _: _:)์ด ํ•จ์ˆ˜๋Š” ๋ฌด์Šจ ์—ญํ• ์„ ํ•˜๊ธธ๋ž˜ ์•ฑ์ด ์–˜๋ฅผ ํ†ตํ•ด ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š”๊ฑธ๊นŒ์š”?
UIApplicationMain(_: _: _: _:) ํ•จ์ˆ˜๋Š” application ๊ฐ์ฒด์™€ ์•ฑ์˜ delegate๋ฅผ ๋งŒ๋“ค๊ณ  ์ด๋ฒคํŠธ ์‹ธ์ดํด์„ ์„ค์ •ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ƒ๊ธด๊ฑด ์•„๋ž˜์ฒ˜๋Ÿผ ์ƒ๊ฒผ์–ด์š”.

func UIApplicationMain(_ argc: Int32, 
                     _ argv: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>, 
                     _ principalClassName: String?, 
                     _ delegateClassName: String?) -> Int32
  • argc : argv์— ์žˆ๋Š” argument์˜ ๊ฐœ์ˆ˜. ๋Œ€๊ฒŒ main๊ณผ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค.
  • argv : argument์˜ ๋ณ€์ˆ˜ ๋ชฉ๋ก. ๋Œ€๊ฒŒ main๊ณผ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค.
  • prinsipalClassName : UIApplicationํด๋ž˜์Šค ๋˜๋Š” ํ•˜์œ„ ํด๋ž˜์Šค์˜ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค. nil์„ ์ง€์ •ํ•˜๋ฉด, UIApplication์œผ๋กœ ๊ฐ€์ •๋ฉ๋‹ˆ๋‹ค.
  • delegateClassName : application delegate๊ฐ€ ์ธ์Šคํ„ด์Šคํ™” ๋˜๋Š” ํด๋ž˜์Šค ์ด๋ฆ„์ž…๋‹ˆ๋‹ค. prinsipalClassName์ด UIApplication์˜ ํ•˜์œ„ํด๋ž˜์Šค๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒฝ์šฐ, ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ delegate๋กœ ์ง€์ • ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์œ„ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋Š” ์•ฑ์˜ delegate ๋ฉ”์„ธ์ง€๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค. ์•ฑ์˜ ๊ธฐ๋ณธ nibํŒŒ์ผ์—์„œ delegate ๊ฐ์ฒด๋ฅผ ๋กœ๋“œํ•˜๋Š” ๊ฒฝ์šฐ, nil์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์‹œ ๋ณธ๋ก ์œผ๋กœ ๋Œ์•„์™€์„œ ๋” ์ง์—ญํ•ด๋ณด์ž๋ฉด,
๊ทธ๋ž˜์„œ ์ด ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด์ง„ ์•ฑ์˜ application ๊ฐ์ฒด๋Š” ๋“ค์–ด์˜ค๋Š” ์‚ฌ์šฉ์ž ์ด๋ฒคํŠธ์˜ ์ดˆ๊ธฐ ๋ผ์šฐํŒ… ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ด๋“ค์€ control ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด(UIControl class์˜ ์ธ์Šคํ„ด์Šค)์ ์ ˆํ•œ ํƒ€๊ฒŸ ๊ฐ์ฒด์— action ๋ฉ”์‹œ์ง€๋“ค์„ ์ „์†กํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, application ๊ฐ์ฒด๋Š” ์—ด๋ฆฐ window(UIWindow์˜ ๊ฐ์ฒด)์˜ ๋ชฉ๋ก์„ ๊ด€๋ฆฌํ•˜๋Š”๋ฐ, ์ด๋ฅผ ํ†ตํ•ด ์•ฑ ๋‚ด์˜ UIView ๊ฐ์ฒด๋“ค์„ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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

๋Œ€๋ถ€๋ถ„์˜ ์•ฑ์—์„œ๋Š” UIApplication์„ ์ƒ์†ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์‹œ์Šคํ…œ๊ณผ ์•ฑ ๊ฐ„์˜ ์ƒํ˜ธ์ž‘์šฉ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด AppDelegate๋ฅผ ํ†ตํ•ด ์ฒ˜๋ฆฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ, ์ƒํ˜ธ์ž‘์šฉ์ด ๋ฌด์กฐ๊ฑด ์‹œ์Šคํ…œ์ด ํ•˜๊ธฐ ์ „์— ์ด๋ฃจ์–ด์ ธ์•ผํ•ด์„œ ์ƒ์†์ด ๋ถˆ๊ฐ€ํ”ผํ•˜๋‹ค๋ฉด, ์ƒ์†์„ ํ•˜๊ณ  sendEvent(_:) ๊ทธ๋ฆฌ๊ณ /ํ˜น์€ sendAction(_:to:from:for:) ๋ฉ”์„œ๋“œ๋ฅผ overrideํ•˜์—ฌ ์ฒ˜๋ฆฌํ•˜๋ฉด๋ฉ๋‹ˆ๋‹ค. ์ถ”์ฒœ๋˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค.


์ด์ œ ์ข…ํ•ฉ์ ์œผ๋กœ ์•ฑ์ด ์‹œ์ž‘๋˜๋Š” ๊ทธ ์‹œ์ ์„ ์ข€ ๋” ์ž์„ธํžˆ ์‚ดํŽด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. ์•ฑ์ด ์‹œ์ž‘๋˜๋Š” ์ˆœ๊ฐ„ AppDelegate์˜ @UIApplicationMain์„ ํ†ตํ•ด UIApplicationMain(_: _: _: _:)๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์‹ฑ๊ธ€ํ†ค์ธ application ๊ฐ์ฒด์™€ delegate๋ฅผ ๋งŒ๋“ ๋‹ค.

    ์ด ๋•Œ, UIApplicationMain(_: _: _: _:)์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์ค‘ delegateClassName์—๋Š” ์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ "AppDelegate"๊ฐ€ ํ• ๋‹น๋œ๋‹ค.

  2. ๊ทธ๋Ÿผ AppDelegate.Swift๋Š” AppDelegate ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๊ณ , ์ด๋ฅผ ์œ„์—์„œ ๋งŒ๋“  application ๊ฐ์ฒด์— ํ• ๋‹นํ•œ๋‹ค.

  3. application ๊ฐ์ฒด๊ฐ€ delegate ๋ฉ”์„œ๋“œ์ธ application:didFinishLaunchingWithOptions: ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

  4. UIApplicationDelegate๋ฅผ ์ฑ„ํƒ์ค‘์ธ AppDelegate๋Š” application:didFinishLaunchingWithOptions:๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

  5. ์•ฑ์ด ์‹คํ–‰๋œ๋‹ค.

ํ•จ๊ป˜ ๋ณด๋ฉด ์ข‹์€ ๋‹ต๋ณ€
์ƒํƒœ ๋ณ€ํ™”์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๋™์ž‘์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ AppDelegate ๋ฉ”์„œ๋“œ๋“ค์„ ์„ค๋ช…ํ•˜์‹œ์˜ค.
SceneDelegate์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜์‹œ์˜ค