Haptics
Haptic feedback is a powerful way to enhance the user experience by providing subtle, tactile responses to user interactions. By leveraging the iPhone's Taptic Engine, developers can create vibrations that reinforce actions, acknowledge success or failure, and improve accessibility. Haptics make an app feel more responsive and engaging, adding a layer of physical interaction that complements visual and auditory cues.
When to Use Haptics
Haptics are best used to reinforce meaningful interactions without being intrusive. Common use cases include:
- Confirmation Feedback – A light tap when toggling a switch or pressing a button.
- Error Alerts – A more pronounced vibration to indicate a failed action.
- Success Indicators – A satisfying pulse when an action is completed.
- Gesture Enhancements – Subtle vibrations when dragging, swiping, or performing complex gestures.
- Accessibility Improvements – Helping users with visual impairments recognize app events.
Implementation
- Integrate with Your View Controller
- Add the following method to
MainViewController.swift
to handle simple haptics:
- Add the following method to
swift
// This method will cause haptics to vibrate
@objc func issueHaptic(_ params: [String: String]) {
let intensity = params["intensity"] ?? "medium"
if intensity == "light" {
let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred(intensity: 1.0)
return
}
if intensity == "medium" {
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred(intensity: 1.0)
return
}
if intensity == "heavy" {
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.impactOccurred(intensity: 1.0)
return
}
}
- Trigger Haptics in JavaScript
- To trigger haptics from JavaScript, call the native
issueHaptic
method:
- To trigger haptics from JavaScript, call the native
javascript
enClose({
nativeCall: 'issueHaptic',
data: {
intensity: 'heavy'
}
});
WARNING
Haptic feedback is only available on iPhones with a Taptic Engine. iPads and Macs do not support haptics.