UIApplication
@MainActor class UIApplication : UIResponder
IOS에서 실행하는 App을 Control하는 객체이다.
모든 IOS앱은 오직 하나의 UIApplication(혹은 UIApplication의 SubClass)의 객체를 가진다.
앱이 실행될때 System이 UIApplicationMain 함수를 호출하고
UIApplicationMain 함수는 UIApplication Class내의 shared라는 class 변수를 사용해 UIApplication 객체를 만든다.
open class var shared: UIApplication { get }
# 참고 #
UIApplicataion은 싱글턴 패턴 방식을 이용해 shared를 통해 객체를 접근하지만
생성자가 private이 아니기 때문에 사실 여러개를 생성할 수 있다.
이는 UIApplication이 private, public 함수가 없는 Object-C로부터 왔기 때문이다.
그러나 Xcode는 UIApplication을 별도로 생성할 경우 Runtime에서 Error를 발생한다.
동작
- User Event 발생시 초기 Routing
- UIControl같은 Control 객체에 의해 전달된 작업메시지를 적절한 Target Object로 전송
- UIWindow Object 목록을 유지 관리 (UIView 객체 검색에 사용)
- 중요한 Runtime 이벤트( ex 메모리부족, 앱의 시작, 종료)를 Delegate에 알려주어 적절한 대응기회 제공
UIApplication의 Subclassing
대부분의 App은 UIApplication을 상속할 필요가 없다.
대신 AppDelegate를 사용해서 System과 App사이에서 상호작용할 수 있기 때문이다.
드문케이스로 App이 System보다 먼저 Event를 처리하길 원할 경우
UIApplication을 상속하여 sendEvent와 sendAction Method를 재정의할 수 있다.
재정의한 함수에서 Event를 가로채 처리한 뒤 super.sendEvent(event)를 호출하여 이후 System이 처리하게 할 수 있다.
UIApplicationDelegate
@MainActor protocol UIApplicationDelegate
UIApplicationDelegate는 UIApplication과 함께 동작하여 System과의 상호 작용을 일부 관리한다.
UIKit은 앱 시작시 UIApplication 객체와, UIAppicationDelegate 객체를 함께 생성하기 때문에 항상 존재한다.
동작
- App의 중앙 Data Structure 초기화
- App의 Scene 구성
- 앱 외부에서 발생하는 Notification에 응답
- App의 Scene, View, ViewController가 아닌 App 자체를 Target으로 하는 Event에 반응
- Apple Push Notification 서비스와 같은 모든 필수 서비스를 등록
IOS 12 이하에서 AppDelegate
IOS12 이하에서 AppDelegate 객체는 App의 주요한 Life Cycle Event를 관리하기 위해 사용된다.
AppDelegate 객체는 App이 Foreground나 Background 상태로 진입할 때 App의 상태를 업데이트하기 위해 사용된다.
(IOS12 이후는 SceneDelegate의 함수가 사용)
Reference
https://developer.apple.com/documentation/uikit/uiapplication
Apple Developer Documentation
developer.apple.com
https://developer.apple.com/documentation/uikit/uiapplicationdelegate
Apple Developer Documentation
developer.apple.com
https://matteomanferdini.com/swift-singleton/
Swift Singletons: A Design Pattern to Avoid (With Examples)
Many developers use Swift singletons to share data in iOS apps. This article shows why you should avoid the singleton pattern and what to use instead.
matteomanferdini.com
https://developer.apple.com/documentation/swift/cocoa_design_patterns
Apple Developer Documentation
developer.apple.com
'IOS > UIKit' 카테고리의 다른 글
[UIKit] UINavigationBar (0) | 2021.10.24 |
---|---|
[UIKit] UINavigation Controller (0) | 2021.10.23 |
[UIKit] App's Life Cycle (0) | 2021.10.13 |