Azhagusundaram Tamil
Azhagusundaram Tamil

Reputation: 2411

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

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. :)

Upvotes: 1

Views: 2941

Answers (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()
            }
        }
    }
}

Upvotes: 1

Asperi
Asperi

Reputation: 258365

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

demo

Upvotes: 1

Related Questions