当应用程序在后台SwiftUI中运行时,是否修改启动视图



我知道,在SwiftUI中,初始启动视图取决于场景代理。所以,有了这样的东西:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
// Create the SwiftUI view that provides the window contents.
//let contentView = ContentView() //only uncomment when messing with user db
let contentView = FirstView()
// let contentView = BarView(value: 3)
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}

我实际上是在告诉Swift打开contentView的rootView,在这种情况下,它等于FirstView((。假设应用程序在之前关闭(即不在后台运行(,每次打开应用程序时都会运行

然而,如果应用程序在后台运行(即,你只需在iPad上按一次主页按钮或其他什么(,用户再次打开它,应用程序就会记住用户最后一次进入的视图,然后继续。有办法改变这种行为吗?通过某种方式,我可以修改"场景代理",这样,当应用程序在后台运行并且用户打开应用程序时,视图将更改为特定视图?

谢谢。

更新,别介意,原谅这个问题。解决方案是将所有代码放入场景代理中的此函数中

func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
print("beans")
}

此代码将在应用程序进入前台时运行。

我不提这个问题,以防对其他人有帮助!

最新更新