My watch app has a page based interface where I don’t really know what InterfaceController is currently on the screen (it could be 1 of 4), however I need to pop an alert no matter what screen the app is on. How can I present an alert given I don't know necessarily which InterfaceController is "current?" The code below will only run if the user has navigated to this InterfaceController. If the user isn't on that page, then I see this error in the console 2019-09-22 15:42:01.597663-0400 Watch Extension[501:526217] Warning: Attempt to present <PUICAlertSheetController: 0x18158c00> on <SPInterfaceViewController: 0x1795e800> whose view is not in the window hierarchy!
extension WorkoutControlsInterfaceController: WorkoutEndedDelegate {
func timerEndedCheckToSeeIfWorkoutEnded(_ manager: WorkoutManager) {
let endWorkoutAction = WKAlertAction(title: "End Workout", style: .default, handler: {
print("User has selected to end the workout")
self.workoutManager?.stopWorkout()
})
let cancelAction = WKAlertAction(title: "Cancel", style: .cancel, handler: {
})
self.becomeCurrentPage()
self.presentAlert(withTitle: "Workout Ended?", message: "It looks like your workout may have ended?", preferredStyle: .alert, actions: [endWorkoutAction, cancelAction])
}
}
You can use the visibileInterfaceController
property from the shared WKExtesion
object;
extension WorkoutControlsInterfaceController: WorkoutEndedDelegate {
func timerEndedCheckToSeeIfWorkoutEnded(_ manager: WorkoutManager) {
let endWorkoutAction = WKAlertAction(title: "End Workout", style: .default, handler: {
print("User has selected to end the workout")
self.workoutManager?.stopWorkout()
})
let cancelAction = WKAlertAction(title: "Cancel", style: .cancel, handler: {
})
WKExtension.shared().visibleInterfaceController?.presentAlert(withTitle: "Workout Ended?", message: "It looks like your workout may have ended?", preferredStyle: .alert, actions: [endWorkoutAction, cancelAction])
}
}