AppDelegate.swift, SceneDelegate.swift 파일은
Xcode 상에서 새로운 프로젝트를 생성할때 만들어지는 파일이며 앱의 상태 변화 및 전역변수 선언, 기타 이벤트 처리 를 할수있는 파일이다.
1. AppDelegate.swift (iOS 13 버전 이전)
- 앱 안에서 상태 변화에 응답하면서 초기에 앱 컨텐트를 그리는 역할을 수행한다 -> iOS 13 버전부터는 일부 기능이 SceneDelegate로 넘어 갔다.
- 생명주기 관련 메소드
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
// 애플리케이션이 실행된 직후 사용자의 화면에 보여지기 전에 호출
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
// 애플리케이션이 최초 실행될때 호출되는 메소드
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
// 애플리케이션이 최초 실행될때 호출되는 메소드
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool
{
return true
}
// 애플리케이션이 InActive 상태로 전환되기 전에 호출
func applicationWillResignActive(_ application: UIApplication) {
}
// 애플리케이션이 백그라운드 상태로 전환시 호출
func applicationDidEnterBackground(_ application: UIApplication) {
}
//애플리케이션이 Active 상태가 되기 직전, 화면에 보여지기 직전에 호출
func applicationWillEnterForeground(_ application: UIApplication) {
}
//애플리케이션이 Active 상태로 전환된 직후 호출
func applicationDidBecomeActive(_ application: UIApplication) {
}
//애플리케이션이 종료되기 직전에 호출
func applicationWillTerminate(_ application: UIApplication) {
}
}
2. AppDelegate.swift -> SceneDelegate.swift(iOS 13 버전 이후)
- SceneDelegate 같은경우 iOS 13 이전에서는 수정하지 않았었던 파일 이였지만 해당 패치 이후로 AppDelegate 에서 사용하던 기능의 일부 를 사용할수 있다. (화면 관련 상태변화)
- iOS 13 부터 AppDelegate가 사용할수 있는 내용은 다음과 같다
1. 전역 변수 초기화
2. 앱 밖에서 발생하는 알림 (배터리 부족, 다운로드 완료등) 반응
3. 앱 푸시알림 서비스 등록
- 생명주기 관련 메소드
import UIKit
import SwiftUI
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
// 애플리케이션이 실행된 직후 사용자의 화면에 보여지기 전에 호출
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let contentView = ContentView()
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
// 애플리케이션이 연결이 해제될때 호출 (종료)
func sceneDidDisconnect(_ scene: UIScene) {
}
// App Switcher 에서 선택 될경우
// App Switcher -> 홈 버튼을 두번 누르거나 아이폰 화면 하단에서 위로 스와이프 했을경우
func sceneDidBecomeActive(_ scene: UIScene) {
}
// 다른 화면으로 전환 했을경우
func sceneWillResignActive(_ scene: UIScene) {
}
// 애플리케이션이 포그라운드로 진입 했을경우
func sceneWillEnterForeground(_ scene: UIScene) {
}
// 애플리케이션이 백그라운드로 짆입 했을경우
func sceneDidEnterBackground(_ scene: UIScene) {
}
}
- 생명주기 관련 메소드 비교 (AppDelegate → SceneDelegate)
3. SceneDelegate 사용 안할경우
1) info.plist 의 Application Scene Manifest 항목 삭제
- Source Code
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<false/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneConfigurationName</key>
<string>Default Configuration</string>
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
</dict>
</array>
</dict>
</dict>
2) SceneDelegate.swift 파일 삭제
3) AppDelegate.swift 에 아래 두 메소드를 삭제한다.
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
4. App State
- Not Running : 실행되지 않았거나, 시스템에 의해 종료된 상태
- Inactive : 실행 중이지만 이벤트를 받고있지 않은 상태. 예를들어, 앱 실행 중 미리알림 또는 일정 얼럿이 화면에 덮여서 앱이 실질적으로 이벤트는 받지 못하는 상태 등
- Active : 어플리케이션이 실질적으로 활동하고 있는 상태.
- Background : 백그라운드 상태에서 동작을 하고 있는 상태. 예를 들어 백그라운드에서 음악을 실행하거나, 걸어온 길을 트래킹 하는 등의 행동이 돌아감.
- suspend : 백그라운드 상태에서 활동을 멈춘 상태. 빠른 재실행을 위하여 메모리에 적재된 상태지만 실질적으로 동작하고 있지는 않음. 메모리가 부족할때 시스템이 강제종료 함
참고 : https://velog.io/@delmasong/App-Delegate
AppDelegate.swift와 UIApplicationDelegate
App Delegate 에 대해 이해하고자 공식 문서를 번역, 정리한 글입니다 (출처)앱의 공유된 동작을 관리하는데 사용하는 일련의 방법.앱 델리게이트 객체는 앱의 공유된 행동을 관리한다. 앱 델리게이
velog.io
https://duwjdtn11.tistory.com/509
[iOS] AppDelegate 그리고 SceneDelegate
AppDelegate & SceneDelegate 오늘은 iOS13 버전 이후 프로젝트 생성시 자동으로 생성되는 AppDelegate.swift 와 SceneDelegate.swift 파일에 대해 알아보고자 한다. iOS 12 까지는 하나의 앱에 하나의 window 구..
duwjdtn11.tistory.com
App and Scenes
앱의 라이프 사이클을 관리하고 여러개의 UI인스턴스 생성에 반응합니다. Overview iOS 13이상에서는 유저가 앱UI의 여러 인스턴스를 동시에 만들고 관리 할 수 있으며, app switcher를 통해 전환도 할
jercy.tistory.com
https://stackoverflow.com/questions/56508764/app-delegate-methods-arent-being-called-in-ios-13
App delegate methods aren't being called in iOS 13
I am using Xcode 11 and building an app for iOS 13. In a new project I created in Xcode some of the UIApplicationDelegate methods were missing so I added them back into the app delegate file. The new
stackoverflow.com
https://zetal.tistory.com/entry/swiftUI-튜토리얼2-SceneDelegate
SceneDelegate란
SceneDelegate란 SceneDelegate을 살펴보겠습니다. Xcode 11부터 SceneDelegate가 기본 iOS 앱 프로젝트 템플릿으로 자동 추가됩니다. SceneDelegate은 swiftUI에만 속해있는 것은 아닙니다. iOS 13 이상에서는..
zetal.tistory.com
'IOS' 카테고리의 다른 글
Swift - iOS 면접 질문 리스트 (0) | 2021.07.18 |
---|---|
Swift - UICollectionView + Paging (0) | 2021.07.13 |
Swift - ReactorKit2 (0) | 2021.07.06 |
Swift - ReactorKit (0) | 2021.06.09 |
Swift - Photos + CollectionView + Drag and Drops (0) | 2021.05.30 |