Tio
Tio

Reputation: 1036

How to use a method when an App is rebooted?

I'm currently developing an application using SwiftUI.

I want to use a method when an App is rebooted at the 4'th process in the process like below:

  1. Build and Run this project.Then a console shows scene sceneWillEnterForeground onAppear sceneDidBecomeActive

  2. Pless the home button and the app goes to the background. then the console shows sceneWillResignActive sceneDidEnterBackground

  3. Double-tap the home button and remove the app screen. Then the console shows Message from debugger: Terminated due to signal 9

  4. press the app icon and reboot the app. Then the console doesn't show anything, and I want to use a method here.

enter image description here

But I can't use any lifecycle method...

Is there any way to do that?


Here are the codes:

SceneDelegate.swift

import UIKit
import SwiftUI

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
     
        let contentView = ContentView()

        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
        
        print("scene")
    }

    func sceneDidDisconnect(_ scene: UIScene) {
       print("sceneDidDisconnect")
    }

    func sceneDidBecomeActive(_ scene: UIScene) {
        print("sceneDidBecomeActive")
    }

    func sceneWillResignActive(_ scene: UIScene) {
       print("sceneWillResignActive")
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
       print("sceneWillEnterForeground")
    }

    func sceneDidEnterBackground(_ scene: UIScene) {
       print("sceneDidEnterBackground")
    }


}

ContentView.swift

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello, World!")
        }.onAppear(){
            print("onAppear")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Xcode: Version 11.7

Swift: Swift 5

Upvotes: 0

Views: 152

Answers (1)

Razvan S.
Razvan S.

Reputation: 803

Well, once you terminate the app, the debugger also disconnects from the app. So there is no way to get any more messages in the console. You would have to restart the app from Xcode so that a new instance with an attached debugger is started. When you do this you're back to step 1.

You could store a timestamp at the moment the app goes to background. Then if you terminate the app and restart it, you can check to see how much time has passed in the application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:) method.

If at most x seconds have passed you can conclude that someone terminated the app and then decided to restart it. If more than x seconds passed or no timestamp is stored, it means that it is just a regular app launch.

Upvotes: 1

Related Questions