Generating Feedback Haptics with UINotificationFeedbackGenerator

A few weeks ago, we talked about how we could play custom haptic feedbacks with CHHapticEngine. We saw how powerful and flexible that class is, letting us create different haptics for any context.

Sometimes though, you want to play simpler haptics to let the user know that something has occurred. The CHHapticEngine class can be overkill, and finding the right parameters to have interaction feedback can be very time consuming.

There is a subclass of UIFeedbackGenerator that actually exists since way before we got all the power CHHapticEngine: UINotificationFeedbackGenerator contains pre-made haptics to let users know when an action finished successfully, with an error, or a “warning” in the context of your app.

Simpler Haptics with UINotificationFeedbackGenerator

In the previous post, we saw how we could configure all the parameters, like the sharpness and intensity. UINotificationFeedbackGenerator, introduced all the way back in iOS 10, is much more simpler than that. In fact, the class has one single method, and that single method can only take three parameters.

Start by creating a UINotificationFeedbackGenerator:

let feedbackGenerator = UINotificationFeedbackGenerator()

And then you can use it as so, calling the notificationOccured(notificationType:) method:

feedbackGenerator.notificationOccured(.success)

And that’s it! notificationOccured(notificationType:) takes a UINotificationFeedbackGenerator.FeedbackType enum, which can take one of the following three values:\

  • error: When an error occurs, it will play a harsh feedback.
  • success: When an operation finishes successfully, it plays a light feedbacl.
  • warning: Feedback in between error and success.

It’s very easy to add this type of feedback, so I recommend you add it whenever it makes sense in your app to have haptics, whether it is something like saving a file successfully, tweeting something successfully, a network error, and so on.

Conclusion

Haptics are a core part of the iOS experience, and implementing them doesn’t have to be complicated. If you need a lot of control over your feedbacks, use CHHapticEngine directly. If you need simple feedbacks that respond to user interaction, the UINotificationFeedbackGenerator class is likely to cover your needs.