How to restore HKWorkoutSession after App relaunch?

I'm developing a running App for WatchOS. I implemented workout restoration as described here: https://developer.apple.com/documentation/watchkit/wkextensiondelegate/2976310-handleactiveworkoutrecovery. Unfortunately, it only works when a crash occurs. When the user closes the App during a workout, the session still remains and there's no way to end it when the user relaunches the App. Is there any way to retrieve the active session and restore the session like HKHealthStore().recoverActiveWorkoutSession?

Accepted Reply

Update: I noticed that handleActiveWorkoutRecovery won't be called after relaunch, but I was able to successfully restore the session by calling recoverActiveWorkoutSession in applicationDidFinishLaunching.

    func applicationDidFinishLaunching() {
             HKHealthStore().recoverActiveWorkoutSession(completion: { session, _ in
            guard let session = session else {
                return
            }
            WorkoutManager.instance.recover(session: session)
        })
    }

Replies

Update: I noticed that handleActiveWorkoutRecovery won't be called after relaunch, but I was able to successfully restore the session by calling recoverActiveWorkoutSession in applicationDidFinishLaunching.

    func applicationDidFinishLaunching() {
             HKHealthStore().recoverActiveWorkoutSession(completion: { session, _ in
            guard let session = session else {
                return
            }
            WorkoutManager.instance.recover(session: session)
        })
    }