Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- 글또
- protocol
- 고차함수
- viewlifecycle
- SWIFT
- error
- http
- 회고
- Refresh
- ScrollView
- mvvm
- list
- self
- Git
- class
- PushNotification
- Switch
- uikit
- array
- 화면전환
- SWIFTUI
- Observer
- NotificationCenter
- singleton
- escaping
- IOS
- struct
- apns
- calendar
- segue
Archives
- Today
- Total
seong_hye, the developer
SwiftUI) App Protocol에 대해 알아보기 본문
UIKit에서는 프로젝트가 만들어지면 AppDelegate와 SceneDelegate가 생성되듯이
https://programming-seonghye.tistory.com/57
UIKit) AppDelegate & SceneDelegate 알아보기
UIKit을 사용하기 위해 프로젝트를 만들게 되면만들어져있는 기능들에 대해 알아보려 한다📘 AppDelegateiOS 앱의 생명 주기(lifeCycle)와 시스템 이벤트를 관리하는 핵심 클래스앱이 실행될 때부터 종
programming-seonghye.tistory.com
SwiftUI에서 생성되는 App에 대해 알아보자
📘App Protocol
iOS 14부터 도입된 앱 생명주기를 선언적으로 정의하는 새로운 진입점(entry point)
@main과 함께 사용되어 앱의 진입점을 명시함
기존의 AppDelegate 또는 SceneDelegate 없이도 앱의 시작 지점과 화면 구성을 직접 정의할 수 있게 해줌
App Protocol = SwiftUI 앱의 시작 지점과 전체 구조를 선언형으로 정의하는 프로토콜
🔹 기본 구조
import SwiftUI
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
🔹 주요 구성 요소
구성 요소 | 설명 |
@main | Swift 앱의 시작 지점 |
App Protocol | 앱 전체의 선언형 구조 정의 |
body | 하나 이상의 Scene을 반환 (WindowGroup, DocumentGroup, Settings) |
WindowGroup | 앱의 기본 UI 윈도우, 여러 개의 창도 지원 (iPad 등) |
🔹 App vs AppDelegate 비교
항목 | App (SwiftUI) | AppDelegate (UIKit) |
앱 진입점 방식 | 선언형 (@main) | 전통적 main.swift |
초기화 시점 | App body 실행 시 | didFinishLaunching |
다중 윈도우 관리 | WindowGroup, Scene 구조로 지원 | SceneDelegate 필요 |
푸시/백그라운드 등 | 별도 AppDelegate 연결 필요 | 내장 메서드로 직접 처리 |
🔹 AppDelegate 대체 또는 연결 가능
SwiftUI에서도 기존 기능( 푸시 알림, 백그라운드 처리 등)이 필요한 경우
기존의 UIApplicationDelegate를 연결할 수 있음
import UIKit
import SwiftUI
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
print("앱 시작됨")
return true
}
}
@main
struct TestApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
'IOS > SwiftUI' 카테고리의 다른 글
SwiftUI) Toast 생성해보기(normal, enum에 따른 toast) (0) | 2022.10.29 |
---|---|
SwiftUI) View Life Cycle (0) | 2022.10.27 |
SwiftUI) 버튼 활용해 화면 맨 위로 올라오기 (0) | 2022.10.18 |
SwiftUI) Picker & DatePicker 사용하기 (0) | 2022.08.31 |
SwiftUI) 하단 Refresh 추가하기 (0) | 2022.08.24 |
Comments