【问题标题】:Is there any way to call SceneDelegate methods in iOS 14 app life cycle?有没有办法在 iOS 14 应用程序生命周期中调用 SceneDelegate 方法?
【发布时间】:2020-07-02 12:38:01
【问题描述】:

我们知道 Apple 已大大简化了 SwiftUI 应用程序生命周期 (WWDC20)。我看过 Paul Hudson 先生关于 SwiftUI 新变化的视频,他在视频中解释了如何在“@main or :App”结构中访问 AppDelegate 方法。

我想知道是否有任何方法可以访问 SceneDelegate 方法?

提前致谢。 :)

【问题讨论】:

    标签: ios swift swiftui ios14


    【解决方案1】:

    现在在 SwiftUI 2.0 生命周期中无法访问 SceneDelegate。一些回调可以与.onChange 一起用于[.active, .inactive, .background] 状态,如for example here

    所示

    或者...继续使用UIKit生命周期没有错

    【讨论】:

    • 感谢@Asperi 的澄清。
    【解决方案2】:

    这是一个在 AppDelegate 中访问生命周期的例子。

    struct PodcastScene: Scene {
        @Environment(\.scenePhase) private var phase
    
        var body: some Scene {
            WindowGroup {
                TabView {
                    LibraryView()
                    DiscoverView()
                    SearchView()
                }
            }
            .onChange(of: phase) { newPhase in
                switch newPhase {
                case .active:
                    // App became active
                case .inactive:
                    // App became inactive
                case .background:
                    // App is running in the background
                @unknown default:
                    // Fallback for future cases
                }
            }
        }
    }
    

    如果您添加像 Firebase 这样的框架配置,您可以覆盖 init 并在此处输入您的代码。

    struct PodcastScene: Scene {
    
        init() {
            FirebaseApp.configure()
        }
    
        var body: some Scene {
            WindowGroup {
                TabView {
                    LibraryView()
                    DiscoverView()
                    SearchView()
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-18
      • 1970-01-01
      • 2018-04-20
      • 2022-06-28
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多