Set up your app to play haptics.
- Core Haptics
This article describes the process of setting up your app to play haptics. You'll check for device compatibility, create a haptic engine, and then configure the engine's handler properties.
Check for Device Compatibility
Not all devices support Core Haptics. For example, iPad and iPod touch don’t support haptic feedback, so calls to Core Haptics won’t result in haptics.
Check for haptics compatibility at the app’s launch, in your app delegate:
By checking for haptic support early using
supports, you can set a global Boolean that your view controllers can quickly consult before deciding to play a haptic. If this variable is set to
NO, then the app can proceed without even attempting a call to Core Haptics. For an example of conditioning haptic playback on this global Boolean, see Playing a Custom Haptic Pattern from a File.
Based on whether or not the user’s device supports haptics, your app can decide to use a different codepath, with stronger audio or other multimedia alternatives for feedback.
Create a Haptic Engine
CHHaptic is your app’s interface to the haptic device. Use an instance of a haptic engine to perform these key tasks:
• Create players to play those haptic and audio patterns.
• Play haptic patterns directly from a file URL.
• Modulate haptic patterns during playback.
Create this engine early in your app’s life cycle—for example, in your main view controller’s
view—and store it in an instance variable or property so it doesn’t go out of scope during playback.
Set the Reset Handler to Recover from Failure
Core Haptics calls the reset handler after the media server has recovered from failure. When this occurs, inside the reset handler, your app should do the following:
Restart the haptic engine, if it was running at the time reset happened, by calling
And Return Error:
Reregister any custom audio resources you had registered, using
Audio Resource: options: error:
Recreate all haptic pattern players you had created, using
Player With Pattern: error:
Your app could attempt to restart the engine inside the handler, allowing it to recover on its own. However, as shown in the code listing above, a restart may still fail if the external reason for the reset hasn't subsided.
Receive Notification of Haptic Engine Stoppage Due to Outside Causes
When external factors cause the haptic engine to stop, like audio interruptions from a phone call or because the user has put your app in the background, Core Haptics calls the
stopped. The reason for the stoppage is passed into the handler. Because stopping is a normal part of the life cycle, you need to restart the engine before it can play the next haptic.
As you're testing your app, set the
stopped to debug the precise cause of the engine stoppage, as shown below.
In production, your app can handle each cause in a different way. For example, you could handle the case
CHHaptic by continuing the app without haptics, or by throwing a fatal error to terminate the app.
Define and Play Haptics
Once you've set up your app to play haptics, you can incorporate haptic patterns. See: