How to add Haptic feedback using Taptic Engine in iOS Swift?

The Taptic Engine is Apple’s implementation of haptic user interface feedback. This is available from iPhone 7 and above. Let’s understand more about it. Taptic Engine is available from iphone 6s, but iphone 7 has a better version of it.

How to do a basic vibration on iOS devices?

AudioToolbox.AudioServices helps us to vibrate the phone using the following code. This works on all devices.

AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))

How to generate haptic feedback?

Reference: Apple Doc link.

class UIFeedbackGenerator : NSObject
  • UISelectionFeedbackGenerator. Use selection feedback generators to indicate a change in selection. It has three variations:.success, .warning, and .error.
  • UINotificationFeedbackGenerator. Use notification feedback generators to indicate successes, failures, and warnings.

Using Feedback Generators

To use a feedback generator, the following are required and this applies to all the above-mentioned feedback generators.

  1. Preparing the Generator (optional): This step is optional. This will reduce the latency (feedback trigger time delay) to some extent. This is particularly important when trying to match feedback to sound or visual cues. Calling the generator’s prepare() method puts the Taptic Engine in a prepared state.
  2. Triggering Feedback: Each feedback generator subclass has a unique triggering method. To trigger feedback, call the appropriate method: impactOccurred(), selectionChanged(), or notificationOccurred(_:). Note that calling these methods does not play haptics directly. Instead, it informs the system of the event. The system then determines whether to play the haptics based on the device, the application’s state, the amount of battery power remaining, and other factors.
  3. Releasing the Generator (optional): Remove all the references so that the system can deallocate the generator instance.

Feedback generators in action!

UIImpactFeedbackGenerator

let impactFeedbackgenerator = UIImpactFeedbackGenerator(style: .light) 
impactFeedbackgenerator.prepare()
impactFeedbackgenerator.impactOccurred()

UISelectionFeedbackGenerator

let selectionFeedbackGenerator = UISelectionFeedbackGenerator()
selectionFeedbackGenerator.selectionChanged()

UINotificationFeedbackGenerator

let notificationFeedbackGenerator = UINotificationFeedbackGenerator()notificationFeedbackGenerator.prepare()notificationFeedbackGenerator.notificationOccurred(.success)
notificationFeedbackGenerator.notificationOccurred(.warning)
notificationFeedbackGenerator.notificationOccurred(.error)

Keep in mind:

  • Always use feedback for its intended purpose. Don’t select a haptic because of the way it feels.
  • The source of the feedback must be clear to the user. For example, the feedback must match a visual change in the user interface, or must be in response to a user action. Feedback should never come as a surprise.
  • Don’t overuse feedback. Overuse can cause confusion and diminish the feedback’s significance.
  • Haptic feedback will work only if the app is running in the foreground, On a device with a supported Taptic Engine and the System-Haptics setting is enabled.
  • As a general rule, trust the system to determine whether it should play feedback. Don’t check the device type or app state to conditionally trigger feedback. After you’ve decided how you want to use feedback, always trigger it when the appropriate events occur. The system ignores any requests that it cannot fulfill.

That’s it. !

Enjoy!!

If you enjoyed reading this post, please share and give some claps so others can find it 👏👏👏👏👏 !!!!

You can follow me on Medium for fresh articles. Also, connect with me on LinkedIn.

iOS and tvOS developer, dreamer, photographer 🤨