NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

How to detect whether haptic event playback is supported

Swift version: 5.2

Paul Hudson    @twostraws   

Modern iPhones have extremely sensitive haptic engines inside, allowing us precise control over taps and vibrations, and even mixing effects – see “How to play custom vibrations using Core Haptics” and “How to modify haptic events over time using CHHapticParameterCurve” for more information on that.

However, not all devices support Core Haptics, which is why you should always check for supported hardware before trying to spin up any haptic feedback. Once you added an import for CoreHaptics, you can detect haptic availability like this:

if CHHapticEngine.capabilitiesForHardware().supportsHaptics {
    // your haptics code here
}

For easier access you could also make the check a computed property, like this:

var hapticsAvailable: Bool {
    CHHapticEngine.capabilitiesForHardware().supportsHaptics
}
Hacking with Swift is sponsored by Instabug

SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred. Instabug's SDK grabs all the logs they need to fix bugs, crashes and performance issues in minutes instead of days. Get screenshots, device details, network logs, repro steps, and tons of other critical insights needed to resolve issues and prioritize product backlogs straight from your dashboard. It only takes a minute to integrate!

Get started now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 13.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.0/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.