问题
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