swiftuiwatchkitwatchos

How to create alarm in SwiftUI on watchOS


I want to create an alarm app for my Apple Watch but I don't know how to wake a person. There is an app - Sleep Cycle and they trigger something that looks like the picture:

Apple Watch with an Alarm dialog window and buttons Open and Stop

The Watch vibrates constantly like the default timer app on Apple Watch. The question is what's the name of this interface element? This is not a notification, an Alert (https://developer.apple.com/documentation/swiftui/alert), or a WKInterfaceDevice play function. I just want to reproduce that behaviour. Thanks for your help!


Solution

  • What you need is notifyUserWithHaptic, it gives a repeating haptic and also displays this notification style screen with Open and Stop buttons if the app isn't active.

    As per Apple Docs

    For schedulable sessions such as smart alarms, call this method during the session to alert the user. When you call the method, the system plays repeating haptic feedback. If the app isn’t active, the system also displays a system alarm alert on the watch.

    Link to Apple Documentation