Is there any way to call SceneDelegate methods in iOS 14 app life cycle?

天涯浪子 提交于 2021-02-11 12:26:48

问题


We know Apple has drastically simplified the SwiftUI app life cycle (WWDC20). I have seen the video of Mr.Paul Hudson about the new changes in SwiftUI in which he has explained how to access AppDelegate methods in "@main or :App" struct.

I want to know if there is any way to access the SceneDelegate methods?

Thanks in advance. :)


回答1:


There is no access for SceneDelegate now in SwiftUI 2.0 life-cycle. Some of callbacks you can use with .onChange for [.active, .inactive, .background] states as shown for example here

or... there is nothing wrong in continue using UIKit life-cycle




回答2:


It's is an example to access life cycle in 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
            }
        }
    }
}

If you add a framework configuration like Firebase, you can override the init and input your code here.

struct PodcastScene: Scene {

    init() {
        FirebaseApp.configure()
    }

    var body: some Scene {
        WindowGroup {
            TabView {
                LibraryView()
                DiscoverView()
                SearchView()
            }
        }
    }
}


来源:https://stackoverflow.com/questions/62696513/is-there-any-way-to-call-scenedelegate-methods-in-ios-14-app-life-cycle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!