์ด์ „์— ์•ฑ์˜ ์‹คํ–‰ ๊ณผ์ •์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ–ˆ์„ ๋•Œ ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜์ง€ ๋ชปํ–ˆ์—ˆ๋‹ค. ์ด๋ฒˆ ๊ธฐํšŒ์— ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜๋Š” ์ž๋ฆฌ๋ฅผ ๊ฐ€์ ธ๋ณด๋ ค ํ•œ๋‹ค. ์กฐ๊ธˆ ์„ฑ์žฅํ–ˆ๋Š”์ง€ ์•ฝ๊ฐ„ ๋” ์ดํ•ด๊ฐ€ ๋˜์—ˆ๋‹ค! ๊ทธ๋Ÿผ ์‹œ์ž‘ํ•˜์ž.

Application Structure

  • Model
    • ๊ฐœ๋ฐœ์ž๊ฐ€ ๋งŒ๋“  ๋ชจ๋ธ
  • Event Loop
    • ํ•˜๋“œ์›จ์–ด ๋‹จ์—์„œ ๋ฐœ์ƒํ•œ ์ด๋ฒคํŠธ๋ฅผ Delegation(์œ„์ž„) ํ•จ
  • Application Delegate
    • App ์ „์ฒด ์ œ์–ด๋ฅผ ๋‹ด๋‹นํ•จ
  • ViewController
    • View ๋‹จ์œ„ ์ œ์–ด ๋‹ด๋‹น
  • UIWindow
    • ๊ฐœ๋ฐœ์ž๋“ค์ด ๋งŒ๋“  View๋“ค์ด ๋ถ™๋Š” ์ตœ์ƒ์œ„ View

UIApplication

// main.swift
 
import UIKit
 
// UIApplication์„ ๋งŒ๋“ฆ
UIApplicationMain(CommandLine.argc, CommandLine.unsageArgv, nil, NSStringFromClass(AppDelegate.self))
  • ์•ฑ์˜ ์‹œ์ž‘
  • ์ „๋‹ฌ ๋ฐ›์€ AppDelegate์˜ ์ด๋ฆ„์„ ์ด์šฉํ•˜์—ฌ AppDelegate์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑ ํ›„ ์—ฐ๊ฒฐ
  • static metatype์„ ๋„˜๊ฒจ์ฃผ๊ณ  ์žˆ๋‹ค.
  • ๋‚ด๋ถ€ ํ•จ์ˆ˜์—์„œ ์ฐพ์•„์„œ ์Šค์Šค๋กœ ์ธ์Šคํ„ด์Šคํ™” ํ•จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
  • [Swift] Metatype ์ด๋ž€? (.Type, .self, .Protocol) (๋ฒˆ์—ญ)
  • Swift ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ํ•ด๋‹น ํŒŒ์ผ์„ ์ž‘์„ฑํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

AppDelegate

// AppDelegate.swift
 
import UIKit
 
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
 
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication,LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }
}
  • @UIApplicationMain ์„ ์–ธ์„ ํ†ตํ•ด main.swift๋ฅผ ์ƒ์„ฑํ•จ
  • ๋”ฐ๋ผ์„œ UIApplicationMain ํ˜ธ์ถœ์ด ํ•„์š”์—†์Œ
  • ํ•ด๋‹น parameter๋กœ AppDelegate ํด๋ž˜์Šค์˜ type์„ ์ž๋™์œผ๋กœ ์ „๋‹ฌ
    • ์‚ฌ์‹ค ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํ•ด์ฃผ๋Š” ๊ฒƒ
  • App ๋‹น 1๊ฐœ์˜ AppDelegate instance ์œ ์ง€
  • ๊ธฐ๋Šฅ
    • App ์ƒํƒœ์— ๋”ฐ๋ฅธ ๋™์ž‘์ฒ˜๋ฆฌ
    • App document์™€ data object์˜ ์ƒ์„ฑ
  • ํŠน์ง•
    • property๋กœ UIWindow๋ฅผ ๊ฐ€์ง
    • ์ด window์œ„์— ํ™”๋ฉด์— ๋ณด์ด๋Š” view๋ฅผ ์˜ฌ๋ฆผ
  • SceneDelegate
    • iOS 13
    • ๊ฐ™์€ program์„ ์—ฌ๋Ÿฌ ํ™”๋ฉด์—์„œ ๋ณด์—ฌ์งˆ ์ˆ˜ ์žˆ๋„๋ก

Application ์‹คํ–‰ ์ƒํƒœ

//์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹คํ–‰๋œ ์งํ›„ ์‚ฌ์šฉ์ž์˜ ํ™”๋ฉด์— ๋ณด์—ฌ์ง€๊ธฐ ์ง์ „์— ํ˜ธ์ถœ 
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool	
 
//์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ตœ์ดˆ ์‹คํ–‰๋  ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ 
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool		
//์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด InActive ์ƒํƒœ๋กœ ์ „ํ™˜๋˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ 
func applicationWillResignActive(_ application: UIApplication)	
 
//์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ƒํƒœ๋กœ ์ „ํ™˜๋œ ์งํ›„ ํ˜ธ์ถœ
func applicationDidEnterBackground(_ application: UIApplication)	
 
//์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด Active ์ƒํƒœ๊ฐ€ ๋˜๊ธฐ ์ง์ „, ํ™”๋ฉด์— ๋ณด์—ฌ์ง€๊ธฐ ์ง์ „์— ํ˜ธ์ถœ 
func applicationWillEnterForeground(_ application: UIApplication)	
 
//์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด Active ์ƒํƒœ๋กœ ์ „ํ™˜๋œ ์งํ›„ ํ˜ธ์ถœ
func applicationDidBecomeActive(_ application: UIApplication)
 
//์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ข…๋ฃŒ๋˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ 
func applicationWillTerminate(_ application: UIApplication)	

Application Structure ์ƒ์„ฑ ์ˆœ์„œ

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    let window = UIWindow.init(frame: UIScreen.main.bounds) // 2.1
 
    window.makeKeyAndVisible() // 2.2
    self.window = window // 2.3
 
    let viewController = ViewController.init() // 3
    self.window?.rootViewController = viewController // 4, 5
 
    return true
}
  1. UIApplicationMain์ด ์‹คํ–‰
  2. Application Delegate ์ƒ์„ฑ
    1. ๋””๋ฐ”์ด์Šค ํ™”๋ฉด ํฌ๊ธฐ๋งŒํผ์˜ window ์ƒ์„ฑ
    2. key window, visible ์„ค์ •
    3. window๋ฅผ appDelegate์— ์„ธํŒ…
  3. AppDelegate์—์„œ Window๋ฅผ property๋กœ ์ƒ์„ฑ
  4. RootViewController ์ƒ์„ฑ
  5. ViewController์˜ View๋ฅผ Window์— ๋ถ™์ž„

Reference