簡體   English   中英

修改應用程序在后台運行時的啟動視圖 SwiftUI?

[英]Modify launch view when app is running in the background 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 或其他任何設備上按一次主頁按鈕),並且用戶再次打開它,應用程序將記住用戶最后進入的視圖,並恢復該視圖。 有沒有辦法改變這種行為? 我可以通過某種方式修改場景委托,以便當應用程序在后台運行並且用戶打開應用程序時,視圖將更改為特定視圖?

謝謝。

更新,沒關系,原諒這個問題。 解決方案是將所有代碼放入場景委托中的 function

    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")
    }

此代碼將在應用程序進入前台時運行。

我會留下這個問題,以防它幫助其他人!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM