0

So suppose I have multiple screens on in my app: View1, View2 etc.

If the user is on a certain screen, say View3, and the application terminates because the user quit the app or such, I know applicationWillTerminate(_ application: UIApplication) will get called, but how do I know which view the user was on before the application terminated?

I need some functionality like this:

func applicationWillTerminate(_ application: UIApplication) {
        let viewUserIsOn = getViewUserWasOnWhenAppQuit() 
        
        switch viewUserIsOn{
        case .view1:
            doThisIfOnView1()
        case .view2:
            doThisIfOnView2()
        default:
            doNothing()
        }
    }
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223

1 Answers1

1

If the views appear sequentially, you can do something like this:

class Settings: ObservableObject {
    static let shared = Settings()

    @Published var viewType: ViewType!
}

enum ViewType {
    case view1, view2, view3
}

struct View1: View {
    @ObservedObject var settings = Settings.shared

    var body: some View {
        [...]
        .onAppear {
            settings.viewType = .view1
        }
    }
}

struct View2: View {
    @ObservedObject var settings = Settings.shared

    var body: some View {
        [...]
        .onAppear {
            settings.viewType = .view2
        }
    }
}

// same for View 3

func applicationWillTerminate(_ application: UIApplication) {
    switch Settings.shared.viewType {
        case .view1: // View 1 was displayed
        case .view2: // View 2 was displayed
        case .view3: // View 3 was displayed
    }
}
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223