Search code examples
swiftswiftuibackgroundapplication-lifecycleios-background-mode

Why my app gets fired immediately after entering background mode?


My SwiftUI app gets fired immediately after entering the background mode.

I replicated the issue in a clean project and after killing all the opened apps and after rebooting the phone:

import SwiftUI

@main
struct testApp: App {
    
    @Environment(\.scenePhase) var scenePhase

    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .onChange(of: scenePhase) { newPhase in
            if newPhase == .active {
                print("Active")
            } else if newPhase == .inactive {
                print("Inactive")
            } else if newPhase == .background {
                print("Background")
            }
        }
    }
}

On the console I have:

2023-09-04 12:21:50 +0000 - Inactive
2023-09-04 12:21:50 +0000 - Active <- swipe to home without killing the app
2023-09-04 12:21:52 +0000 - Inactive
2023-09-04 12:21:53 +0000 - Background
2023-09-04 12:21:53 +0000 - Message from debugger: Terminated due to signal 9

I don't need background tasks, just the possibility to reopen the app on the last state for at least 10 seconds after the user move from my app to another and then comes back to the app.

  • Macbook M1
  • Xcode 14.3.1
  • iPhone 13 (iOS 16.6)
  • Deployment target: 16.4
  • Swift Language: 5

EDIT: I didn't have the problem on the simulator. I can reproduce only on a phisical device.


Solution

  • Solved! on phone Developer settings in "State Restoration testing" there was "Fast App Termination" set to ON.