ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • UIApplicationDelegateAdaptor
    SwiftUI/SwiftUI 애플 문서 한글화 2023. 6. 5. 13:51

     

    Structure

    이 문서는 https://developer.apple.com/documentation/swiftui/uiapplicationdelegateadaptor 를 한글화한 문서입니다.

    UIApplicationDelegateAdaptor

    UIKit의 app delegate를 생성하는 데 사용하는 레퍼타입의 프로퍼티

     

    Declaration

    @MainActor @propertyWrapper 
    struct UIApplicationDelegateAdaptor<DelegateType> where DelegateType : NSObject, DelegateType : UIApplicationDelegate

    Overview 

    SwiftUI 수명 주기를 사용하는 앱에서 app delegate 콜백을 처리하려면 UIApplicationDelegate 프로토콜을 준수하는 클래스를 정의하고 필요한 delegate 메서드를 구현합니다. 예를 들어 원격 알림 등록을 처리하기 위해서. application(_: didRegisterForRemoteNotificationWithDeviceToken:) 메서드를 구현할 수 있습니다.

    class MyAppDelegate: NSObject, UIApplicationDelegate, ObservableObject {
        func application(
            _ application: UIApplication,
            didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
        ) {
            // Record the device token.
        }
    }

    그런 다음 앱을 선언할 때 UIApplicationDelegateAdaptor 속성 래퍼를 사용하여 SwiftUI에 해당 클래스가 app delegate 타입임을 알립니다. 

    @main
    struct MyApp: App {
        @UIApplicationDelegateAdaptor private var appDelegate: MyAppDelegate
    
    
        var body: some Scene { ... }
    }

    SwiftUI는 delegate를 인스턴스화하고 수명 주기 이벤트에 따라 delegate 메서드를 호출합니다. 앱  선언부에서만 app delegate 어뎁터를 지정한 앱에 한번만 정의합니다. 두 번 이상 정의한 경우 SwiftUI는 런타임 오류를 만듭니다. 앱 딜리게이터를 ObservableObject 프로토콜을 준수하게 하면, SwiftUI는 생성한 딜리케이터를 환경 객체로 사용할 수 있습니다. 이를 통해 EnvironmentObject 속성 래퍼를 사용하여 앱의 모든 Scene이나 뷰에서 딜리게이터에 엑세스 할 수 있게 됩니다. 

    @EnvironmentObject private var appDelegate: MyAppDelegate

    이렇게 하면 $ 접두사를 사용하여 delegate에서 선언한 게시된 속성에 대한 바인딩을 가져올 수 있습니다. 자세한 사항은 projectedValue를 참조하세요.

    important
    가능하면 앱 딜리게이터를 사용하지 않고 앱의 수명 주기 이벤트를 관리하는 게 좋습니다. application(_: didFinishLaunchingWithOptions:)과 같은 콜백에 의존하는 대신 ScenePhase에서 변경 사항을 처리하는 것이 좋습니다.

    Scene Delegate

    일부 iOS 앱은 UIWindowSceneDelegate를 정의하여 앱 바로가기 와 같은 scene 기반 이벤트를 처리합니다.

    class MySceneDelegate: NSObject, UIWindowSceneDelegate, ObservableObject {
        func windowScene(
            _ windowScene: UIWindowScene,
            performActionFor shortcutItem: UIApplicationShortcutItem
        ) async -> Bool {
            // Do something with the shortcut...
    
    
            return true
        }
    }

    앱 delegate 타입의 application(_:configurationForConnecting:options:)메서드에서 Scene delegate 타입을 반환하도록 해 scene delegate를 제공할 수 있습니다. 

    extension MyAppDelegate {
        func application(
            _ application: UIApplication,
            configurationForConnecting connectingSceneSession: UISceneSession,
            options: UIScene.ConnectionOptions
        ) -> UISceneConfiguration {
    
    
            let configuration = UISceneConfiguration(
                                    name: nil,
                                    sessionRole: connectingSceneSession.role)
            if connectingSceneSession.role == .windowApplication {
                configuration.delegateClass = MySceneDelegate.self
            }
            return configuration
        }
    }

    UISceneConfiguration 인스턴스를 구성할 때, scene 클래스나 스토리보드가 아닌 Scene Delegate만 지정하면 됩니다. SwiftUI는 delegate 인스턴스를 생성 및 관리하고, 콜백 함수를 호출합니다. 

     

    앱 델리케이트와 마찬가지로 씬 델리케이트를 관찰 가능한 객체로 만들면 SwiftUI는 자동으로 EnvironmentObject 속성 레퍼를 사용하여 엑세스 할 수 있는 환경 변수를 배치하고 게시된 속성에 대한 바인딩을 생성합니다. 

    Topics

    딜리케이터 어뎁터 생성 

     

    init(DelegateType.Type)

    Creates a UIKit app delegate adaptor.

    init(DelegateType.Type)

    Creates a UIKit app delegate adaptor using a delegate that’s an observable object.

    Available when DelegateType inherits NSObject, DelegateType conforms to ObservableObject, and DelegateType conforms to UIApplicationDelegate.

    Getting the delegate adaptor

    var projectedValue: ObservedObject<DelegateType>.Wrapper

    A projection of the observed object that provides bindings to its properties.

    Available when DelegateType inherits NSObject, DelegateType conforms to ObservableObject, and DelegateType conforms to UIApplicationDelegate.

    var wrappedValue: DelegateType

    The underlying app delegate.

     

    댓글

Designed by Tistory.