์ด์ ์ ์ฑ์ ์คํ ๊ณผ์ ์ ๋ํด์ ๊ณต๋ถํ์ ๋ ์ ๋๋ก ์ดํดํ์ง ๋ชปํ์๋ค. ์ด๋ฒ ๊ธฐํ์ ์ ๋๋ก ์ดํดํ๋ ์๋ฆฌ๋ฅผ ๊ฐ์ ธ๋ณด๋ ค ํ๋ค. ์กฐ๊ธ ์ฑ์ฅํ๋์ง ์ฝ๊ฐ ๋ ์ดํด๊ฐ ๋์๋ค! ๊ทธ๋ผ ์์ํ์.
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๋ฅผ ์ฌ๋ฆผ
- property๋ก
- 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
}
- UIApplicationMain์ด ์คํ
- Application Delegate ์์ฑ
- ๋๋ฐ์ด์ค ํ๋ฉด ํฌ๊ธฐ๋งํผ์ window ์์ฑ
- key window, visible ์ค์
- window๋ฅผ appDelegate์ ์ธํ
- AppDelegate์์ Window๋ฅผ property๋ก ์์ฑ
- RootViewController ์์ฑ
- ViewController์ View๋ฅผ Window์ ๋ถ์