1

I have a timer for my watch that saves the start time to UserDefaults so that when app comes back from background phase or on inactive phase I get an accurate time. However, I dont want the timer's start time to save if the app is closed by user. So how do I detect when the user closes the app so that I can implement something that will not save the start time.

I tried making something similar to https://stackoverflow.com/a/63765784 with the addition of UIApplication.willTerminateNotification but I get the error:

UIApplication is not in scope.

Importing UIKit gives the same error, and I get tbe same error when I try:

.onReceive(NotificationCenter.default.publisher(for: UIApplication.willTerminateNotification), perform: { output in
                    //code here
         })

I also saw this post but its answers don't seem to answer my question. How can detect watch os is ios app closed SWIFTUI

Fanfan
  • 11
  • 4

0 Answers0