How to make iPhone vibrate using Swift?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Making an iPhone vibrate in Swift looks easy, but there are several APIs with different behavior, capabilities, and OS requirements. Older code uses system vibration services from AudioToolbox. Modern apps often use UIFeedbackGenerator for lightweight haptics or CoreHaptics for custom patterns. The best API depends on your use case: simple alert feedback, button tap feedback, or a fully designed haptic sequence.
A robust implementation should also consider device capability, user settings, and context. For example, some haptic patterns are unavailable on older devices, and overly frequent vibration can hurt usability. Good haptics are brief, intentional, and tied to meaningful UI state changes.
Core Sections
1. Quick vibration with AudioToolbox
For a basic vibration trigger, this is still the smallest API surface:
This works for straightforward alert feedback, but it has limited customization and no nuanced intensity control.
2. Prefer UIFeedbackGenerator for UI interactions
For button taps, success states, or warnings, use UIKit feedback generators. They integrate better with iOS interaction patterns.
Call prepare() shortly before the event to reduce latency. Do not create generators repeatedly in hot paths; reuse when appropriate.
3. Create custom haptic patterns with CoreHaptics
If you need richer tactile design, use CoreHaptics (iOS 13+). Always check capability first.
This gives precise control over intensity and timing for brand-specific tactile behavior.
4. Integrate with app state intentionally
Trigger haptics on meaningful transitions: completed payment, failed form submission, drag snap, confirmation. Avoid vibrating on every minor state change. If feedback is coupled to async operations, trigger on final result, not on request start.
5. Test on real devices
Simulators do not reproduce physical haptic behavior reliably. Validate rhythm, perceived strength, and event timing on target hardware.
Common Pitfalls
- Using only
AudioToolboxfor modern UI feedback whereUIFeedbackGeneratorgives better semantics. - Triggering haptics too frequently, causing noisy and fatiguing interactions.
- Ignoring capability checks before using
CoreHaptics, which breaks on unsupported devices. - Creating new feedback generator objects for every tap in tight loops, increasing latency.
- Testing exclusively in simulator and shipping unvalidated tactile behavior.
Summary
In Swift, iPhone vibration can be implemented at three levels: simple system vibration (AudioToolbox), interaction-level haptics (UIFeedbackGenerator), or fully custom patterns (CoreHaptics). Choose the lightest API that matches your product need, gate advanced patterns by hardware capability, and trigger feedback only for meaningful events. With these practices, haptics become a useful part of UX rather than an afterthought.
A practical way to keep this issue from returning is to turn the fix into a lightweight runbook. Capture the exact environment assumptions (tool versions, runtime flags, cluster or platform settings, and required dependencies), then store a short verification command sequence that any teammate can run from a clean setup. This makes troubleshooting deterministic instead of person-dependent and reduces rework during on-call incidents.
It also helps to add one automated guardrail in CI or pre-deploy checks that validates the critical assumption described above. That guardrail might be a linter rule, a smoke test, a schema check, a policy validation step, or a minimal integration test. When the same class of failure is caught before release, teams spend less time on emergency debugging and more time on controlled improvements.
Related reading
- How to make layout with View fill the remaining space?
- How to make links in a TextView clickable
- How to make links in a TextView clickable
- How to make phone call in iOS 10 using Swift?
- How to make primary key as autoincrement for Room Persistence lib
- How to make return key on iPhone make keyboard disappear?
- How to make space between LinearLayout children?
- How to make TextField become the first responder
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.